無法理解Angular JS中的控制器功能 - Unable to understand the controller functionality in Angular JS -开发者知识库

無法理解Angular JS中的控制器功能 - Unable to understand the controller functionality in Angular JS -开发者知识库,第1张

I am new to Angular JS.I am trying to gain some in depth knowledge in it by understanding the controller functionality.

我是Angular JS的新手。我試圖通過了解控制器功能來獲得一些深入的知識。

I came across the following code snippets.

我遇到了以下代碼片段。

var App = angular.module('clientApp', ['ngResource', 'App.filters']);
 App.controller('ClientCtrl', ['$scope',function ($scope) {

   }]);

What significant difference does it make if i write :

如果我寫的話會有什么顯着的不同:

 var App = angular.module('clientApp', ['ngResource', 'App.filters']);
 App.controller('ClientCtrl', function ($scope) {

   });

My Understanding: I do understand that something written in the square brackets is dependency for that particular module or controller.However,I couldnot understand the reason for writing

我的理解:我確實理解方括號中的內容是對特定模塊或控制器的依賴。但是,我無法理解寫作的原因

          "['$scope',function($scope)" 

instead of

   App.controller(controllername,function($scope){

   });

Any help wouls be highly appreciated!

任何幫助都會受到高度贊賞!

2 个解决方案

#1


1  

It enables AngularJS code to be minified. AngularJS uses parameter names to inject the values to your controller function. In JavaScript minification process, these parameters are renamed to shorter strings. By telling which parameters are injected to the function with a string array, AngularJS can still inject the right values when the parameters are renamed.

它使AngularJS代碼縮小。 AngularJS使用參數名稱將值注入控制器函數。在JavaScript縮小過程中,這些參數將重命名為較短的字符串。通過告訴使用字符串數組向函數注入哪些參數,AngularJS仍然可以在重命名參數時注入正確的值。

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复