0

ng-app="SomethingApp" - this what I have in the body of the master page. Now different pages have different controllers. It might be 1 or more on the page. Now, on the master page, I need to do the frequent changes within the controllers once their content is loaded. I used this code

var someFunction = function () {

    var injector = angular.element(document.querySelector('[SomethingApp]')).injector();
    var someService = injector.get('someService');
    var $rootScope = injector.get('$rootScope');

    someService.someAjaxFunction(someAreaKey, true).then(function (someItems) {
        for (i = 0, len = someItems.length; i < len; i++) {
            var someItem = someItems[i];
            var elem = angular.element(document.querySelector("'#" + someItem.Key + "'"))[0];
            elem.innerText = someItem.Value;
        }
    }, function (data) {
        $rootScope.Raw = data.status;
    });

    $rootScope.$digest();
}

angular.element(document).ready(function () {
    someFunction();
});

Everything is working except one thing. When a document is ready, it doesn't mean the controllers are loaded. So, elements I am looking for to do the changes doesn't exist yet.

How can I identify when all the controllers are loaded?

Gonzalo.-
  • 12,512
  • 5
  • 50
  • 82
  • 3
    Looking for elements in the DOM is not really "the angular way". Typically you bind the content of the element to a model, then update the model whenever you need to. Sounds like you might need to rethink your strategy. – Heretic Monkey Mar 07 '17 at 18:28
  • Also, you might find [this post](http://stackoverflow.com/questions/11252780/whats-the-correct-way-to-communicate-between-controllers-in-angularjs) useful. – lealceldeiro Mar 07 '17 at 18:36
  • Agree, but it is the still the requirement. If it cannot be done, then there is no other choice to rethink it. However, I won't loose a hope that there is an answer for that. – Pavel Brokhman Mar 07 '17 at 18:37
  • why do you need to know if a controller file has been loaded? it feels like it should be better to solve this in angular context, than rather on checking if files have been loaded. What are you trying to achieve? – Gonzalo.- Mar 07 '17 at 20:26
  • As others have mentioned, you may just want to rethink your code. That being said, posting the full code certainly wouldn't hurt. Generally speaking, you want to put any DOM manipulation inside of a directive `link` method. – Pytth Mar 07 '17 at 21:31

0 Answers0