0

Why I can use ng-bind outside of ng-app and ng-controller when using $rootScope.variable?

faraWorkspaceApp.run(function ($rootScope, $location, $state) {
    $rootScope.$on('$stateChangeSuccess', function (e, toState, toParams
                                                   , fromState, fromParams) {
        $rootScope.pageTitle = toState.pageTitle;
    });
});

<span ng-bind="pageTitle"></span>
<div ng-app>
</div>
Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93
  • could you reproduce the problem with plunkr or fiddle? – Pankaj Parkar Apr 29 '15 at 06:19
  • ng-app is the directive for root element. Angularjs will only process element in ng-app – hutingung Apr 29 '15 at 06:24
  • I think you should put ng-app on tag as it will allow you to use angular js framework for entire app. It is not possible to use a component outside its framework bounds. $rootScope allows you to use a variable in all controllers, services, factories, filters and configs but that doesn't mean you can use it outside bound of a framework – Yashika Garg Apr 29 '15 at 06:52

1 Answers1

1

$scopes are tied to an controller, without the controller they can't be put into your view. $rootScope is tied to your ng-app, so in actual fact you can't use it outside of ng-app but you can use it outside of ng-controller.

$scopes have an inheritance model, which means child $scopes will automatically get the value of the parent $scope.

yangli-io
  • 16,760
  • 8
  • 37
  • 47