0

Is it possible to get the specific scope of an object by accesing it through an ID?

I use angular ui tree for a navigation menu. After I add a subitem and save the navigation into a mysql database, the node with the subitem is not expanded. After the push, I see that the menu expands, but after saving it in the database and rerendering the navigation menu, the node is not expanded.

Have someone a solution?

JSON-Object of the menu.

[
  {
    "id": 35,
    "title": "foo",
    "items": []
  },
  {
    "id": 34,
    "title": "bar",
    "items": [
      {
        "id": 123,
        "title": "foobar",
        "items": []
      },
    ]
  },
  {
    "id": 46,
    "title": "barfoo",
    "items": []
  },
]

This structure is saved with the following code

function setMenu(nodeID) {
        $http.post("...", {
            "menuJSON" : $scope.menu
        }).success(function (data, status, headers, config) {
            if (status == 200) {
                loadAdminNavigation();
            }
        });
    };

After saving it in the mysql database, the structure is reloaded with this code

function loadAdminNavigation() {
        $http.get("...").success(function (response) {
            $scope.menu = response;
        });
    };

If I add a SubItem, all scopes are new, I want to expand the node with the ID from the model. So I need to know how I can access the scope via the ID.

  • Can you share the code you have tried so far? – Jax Jul 22 '15 at 15:14
  • If you mean scope from an element id ... yes...but issue sounds more like a problem in object structure being saved. Far from enough information given to help with. A comparison of working data to data you save would help – charlietfl Jul 22 '15 at 15:21

2 Answers2

0

Example code to get the scope of an element:

var documentResult = document.getElementsById("someId");
angular.element(documentResult).scope();

Official Angular Docs about element:

https://docs.angularjs.org/api/ng/function/angular.element

scope() - retrieves the scope of the current element or its parent. Requires Debug Data to be enabled.

Can read that post: How do I access the $scope variable in browser's console using AngularJS?

Community
  • 1
  • 1
nada
  • 972
  • 5
  • 22
0

You can do it easily:

angular.element(document.querySelector( '#myId' )).scope();

or even:

angular.element( '#myId' ).scope();

Cheers!

graham o'donnel
  • 385
  • 2
  • 5
  • 18