What's the difference between the two controll

2019-02-20 05:45发布

This question already has an answer here:

When I run the code, Option A is failed but not sure what the reason is.

angular.module('app').controller('controllerA', function($scope, $http) {
      // code
}

angular.module('app').controller('controllerB', ['$scope', '$http', function($scope, $http) {
     // code
}]);

I tried to test it on Angular 1.x JSFiddle, but both of them work well. Help me!

3条回答
ゆ 、 Hurt°
2楼-- · 2019-02-20 06:16

This makes much difference in the minification process. if you are using angular.min.js library then in the production, option A fails. in JSFiddle u might use angular.js lib. That's why both scenarios work.

Check this for more information about minification

查看更多
放荡不羁爱自由
3楼-- · 2019-02-20 06:16

try below example

var myApp = angular.module("app", []);
myApp.controller("controllerA", function ($scope) {

});
myApp.controller("controllerB", function ($scope) {

});
查看更多
Root(大扎)
4楼-- · 2019-02-20 06:21

Both will work, but you should follow the second example, which uses the Array syntax so you can minify your code without worrying about the minifier renaming function parameters

查看更多
登录 后发表回答