我有一个简单的 angularjs 指令,当我缩小文件时,我收到错误,因为变量名称已更改
var app= angular.module('app',[]);
app.directive('directive',function(){
return {
restrict: 'EA',
scope: {},
replace: true,
link: function($scope, element, attributes){
$scope.name="test-test";
,
controller: function($scope,$attrs,$http){
},
templateUrl: 'directives/app/app.tpl.html'
}
});
问题是 $scope.name 更改为 a.name 并且 Angular 无法识别它。 我尝试通过尝试类似的方法来注入(inject) $scope
link: ['$scope',function($scope, element, attributes){
$scope.name="test-test";
}],
controller: ['$scope','$attrs','$http',function($scope,$attrs,$http){
}],
但是缩小后我仍然遇到与 a.name 相同的错误。
最佳答案
指令link
函数不会被注入(inject)。它们传递了一组固定的参数,这些参数在 angular.js documentation 中列出并进行了全面描述。 。然而, Controller 的情况并非如此。这些是注入(inject)的,并且应该在缩小之前进行注释。您至少可以通过 3 种方式做到这一点:
- 使用像示例中那样的数组语法
- 在 Controller 函数上设置
$inject
属性,其值为可注入(inject)名称数组 - 使用 ngAnnotate 进行注释它将检测 Angular 注入(inject)的使用并正确注释它
关于javascript - 如何正确缩小 angularJS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39086318/