javascriptangularjsng-bindng-app

Why I can use `ng-bind` outside of `ng-app`?


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>

Solution

  • $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.