什么是角括号使用的目的是什么? [英] What is the purpose of square bracket usage in Angular?

查看:3844
本文介绍了什么是角括号使用的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想明白 MyOtherService 的声明之间的区别 MyOtherComplexService 。特别的是方形支架部分的目的是什么?何时使用它们,何时不?

VAR的myapp = angular.module('MyApp的',[]);myapp.factory('为MyService',函数($ rootScope,$超时){
    返回{
        富:功能(){
            返回为MyService
        }
    }
});myapp.factory('MyOtherService',函数($ rootScope,$超时,为MyService){
    返回{
        富:功能(){
            返回MyOtherService
        }
    }
});myapp.factory('MyOtherComplexService',['$ rootScope','$超时','为MyService',函数($ rootScope,$超时,为MyService){
    返回{
        富:功能(){
            返回MyOtherComplexService
        }
    }
}]);myapp.controller('myController的',函数($范围,MyOtherService,MyOtherComplexService){    $ scope.x = MyOtherService.foo();
    $ scope.y = MyOtherComplexService.foo();
});


解决方案

它使得AngularJS code将精缩。 AngularJS使用参数名的值注入到控制器的功能。在JavaScript缩小过程中,这些参数被重命名为更短的字符串。通过讲述这些参数被注入到函数的字符串数组,AngularJS仍然可以注入正确的价值观,当参数被重命名。

I would like to understand the difference between the declaration of MyOtherService and MyOtherComplexService. Especially what is the purpose of square bracket part? When to use them and when not?

var myapp = angular.module('myapp', []);

myapp.factory('MyService', function($rootScope, $timeout) {
    return {
        foo: function() {
            return "MyService";
        }
    }
});

myapp.factory('MyOtherService', function($rootScope, $timeout, MyService) {
    return {
        foo: function() {
            return "MyOtherService";
        }
    }
});

myapp.factory('MyOtherComplexService', ['$rootScope', '$timeout', 'MyService', function($rootScope, $timeout, MyService) {
    return {
        foo: function() {
            return "MyOtherComplexService";
        }
    } 
}]);

myapp.controller('MyController', function($scope, MyOtherService, MyOtherComplexService) {

    $scope.x = MyOtherService.foo();
    $scope.y = MyOtherComplexService.foo(); 
});

解决方案

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.

这篇关于什么是角括号使用的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆