1

My html...

<body id="main">
  {{pageName}}
</body>

My JS:

angular.module('myApp',[])
  .controller('myController', function($scope){
   console.log('init');
   $scope.pageName = 'My page';
});

angular.element(document).ready(function(){
   angular.bootstrap(document.getElementById('main'), ['myApp']);
});

My Resulting HTML:

{{pageName}} instead of 'My Page'

I can do this

<body id="main" ng-controller="myController">
</body>

and it will start working. But, what is the point. Why do I necessarily have to use ng-controller ?

I hope I have made myself clear. Hoping someone would respond to this.

1 Answer 1

1

I think the reason is that you are setting pageName in myController but the div cannot use it as the controller is neither defined on it or its parent.

pageName should be set on $rootScope to make it work. You can define that at the run block of module like

angular.module('myApp',[]).run(function($rootScope) {
  $rootScope.pageName='My Page';
});

This run block runs after angular has bootstrapped the application. See here
http://docs.angularjs.org/guide/module

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.