Declaring controllers in AngularJS

前端 未结 3 616
不知归路
不知归路 2020-12-19 00:54

I\'ve seen that on AngularJS tutorials, that some people declare their controller functions like so:

function FirstController($scrope) {
    // do something          


        
相关标签:
3条回答
  • 2020-12-19 01:14

    The recommended way of declaring Controllers is using the array notation:

     someModule.controller('MyController', ['$scope', 'dep1', 'dep2', function($scope,  dep1, dep2) {
         ...
         $scope.aMethod = function() {
         ...
        }
      ... 
    }]); 
    

    according to angularJS site : https://docs.angularjs.org/guide/di

    0 讨论(0)
  • 2020-12-19 01:20

    You should follow the second example they offer, which uses a string to identify your controller rather than a potentially global function. Use the Array syntax so you can minify your code without worrying about the minifier renaming function parameters.

    var myApp = angular.module('myApp');
    
    myApp.controller('GreetingCtrl', ['$scope', function($scope) {
        $scope.greeting = 'Hola!';
    }]);
    

    Source: http://docs.angularjs.org/guide/controller

    0 讨论(0)
  • 2020-12-19 01:25
    myApp.controller('myControl',['$scope',function($scope){
        $scope.controlname = "Something";
    }]);
    
    0 讨论(0)
提交回复
热议问题