search

Home  >  Q&A  >  body text

angular.js - About the problem of calling and using global variables after defining them in angularjs

The following is the method I found to define global variables (please point out if there is any misunderstanding):
1,myApp.run(function($rootScope) {

$rootScope.haha = 'test';

});

2.<p ng-app="myApp" ng-init="haha='test';">

3.app.value(key, value)

4.app.constant(key, value)

I found that there are only 1 and 2. I can directly output it in the template: {{haha}}, but I don’t know how to call it in the controller?
Then 3 and 4, I can’t output them in the template, let alone in the controller.

Thank you!

黄舟黄舟2775 days ago626

reply all(1)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-05-15 17:02:20

    1

    app.controller('controller', function($scope, $rootScope) {
        $scope.haha = $rootScope.haha
    })
    

    3/4
    http://hellobug.github.io/blog/angularjs...

    reply
    0
  • Cancelreply