문제

I need to pass some variables from backend to Angular frontend in HTML code. Ideally, I like to have it within $rootScope. However, $rootScope is not defined yet when the page is loaded. The only way I can do it is:

var myVar = ["aa", "bb"];

Then get it from $window.myVar

So is there a way to wait until $rootScope is defined by Angular and then parse value into it within HTML?

도움이 되었습니까?

해결책

How about defining a constant and injecting it where you need it?

angular.module('yourModule').constant('yourVar', ['aa', 'bb']);

Then you can use it in the run stage:

angular.module('yourModule').run(function($rootScope, yourVar) {
    $rootScope.yourVar = yourVar;
});

다른 팁

Yep, try something like this

var myApp = angular.module('App', []);

myApp.run(function($rootScope) {
   $rootScope.myVar = ['aa', 'bb'];
});

// we launch Angular manually when DOM is ready
angular.element(document).ready(function() {
   angular.bootstrap(document, ['App']);
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top