AngularJS:缩小后指令中的范围无法访问
AngularJS : Scope inaccessible in directive after minification
在指令内的 link 函数中访问范围是 "undefined" 在对许多示例进行缩小后(使用 Closure Compiler),但预缩小工作正常。
例如,Angular 教程中的以下代码经过一些改动。缩小后 $scope.format 在任何时候都不会被指令选中。该指令显示默认格式(2015 年 7 月 15 日),没有任何错误。在缩小之前,指令显示 $scope.format (7/15/2015 12:09:04 AM) 中定义的格式。
app.js
.controller('Controller', ['$scope', function ($scope) {
$scope.format = 'M/d/yy h:mm:ss a';
}])
.directive('myCurrentTime', bd.myCurrentTime.Directive.factory)
我的当前时间-directive.js
'use strict';
goog.provide('bd.myCurrentTime.Directive.factory');
/**
* Example directive factory.
*
* @return {Object}
* @ngInject
* @export
*/
bd.myCurrentTime.Directive.factory = function ($interval, dateFilter) {
function link(scope, element, attrs) {
var format,
timeoutId;
function updateTime() {
element.text(dateFilter(new Date(), format));
}
scope.$watch(attrs.myCurrentTime, function (value) {
format = value;
updateTime();
});
element.on('$destroy', function () {
$interval.cancel(timeoutId);
});
// start the UI update process; save the timeoutId for canceling
timeoutId = $interval(function () {
updateTime(); // update DOM
}, 1000);
}
return {
link: link
};
};
// Added by ng-annotate
bd.myCurrentTime.Directive.factory.$inject = ["$interval", "dateFilter"];
html 文件:
<div ng-controller="Controller">
Date format: <input ng-model="format"> <hr />
Current time is: <span my-current-time="format"></span>
</div>
您使用的是 Closure 编译器的高级编译模式吗?没有看到你的缩小代码很难查明问题,但这里有一些想法:
在这部分代码中:
.controller('Controller', ['$scope', function ($scope) {
$scope.format = 'M/d/yy h:mm:ss a';
}])
$scope.format
可能会重命名为类似 s.f
的名称。通常,这不是问题,特别是因为 $scope
被正确注入。但是,您在 HTML 中引用了 format
属性,Closure 编译器不知道:
Date format: <input ng-model="format"> <hr />
Current time is: <span my-current-time="format"></span>
尝试改用 $scope['format'] = 'M/d/yy h:mm:ss a'
- Closure 编译器从不更改字符串,因此这应该为 HTML 导出正确的 属性 名称。现在,记住 - once you use a string to access a property, always use a string to access that property.
与#1 类似,attrs.myCurrentTime
可能会重命名为 a.m
。尝试使用字符串 属性 名称 (attrs['myCurrentTime']
) 以确保编译的 JavaScript 正确匹配 HTML。
同样,这些只是此时的想法。如果您 post 将缩小的代码作为完整示例,我们将能够提供更多帮助。
在指令内的 link 函数中访问范围是 "undefined" 在对许多示例进行缩小后(使用 Closure Compiler),但预缩小工作正常。
例如,Angular 教程中的以下代码经过一些改动。缩小后 $scope.format 在任何时候都不会被指令选中。该指令显示默认格式(2015 年 7 月 15 日),没有任何错误。在缩小之前,指令显示 $scope.format (7/15/2015 12:09:04 AM) 中定义的格式。
app.js
.controller('Controller', ['$scope', function ($scope) {
$scope.format = 'M/d/yy h:mm:ss a';
}])
.directive('myCurrentTime', bd.myCurrentTime.Directive.factory)
我的当前时间-directive.js
'use strict';
goog.provide('bd.myCurrentTime.Directive.factory');
/**
* Example directive factory.
*
* @return {Object}
* @ngInject
* @export
*/
bd.myCurrentTime.Directive.factory = function ($interval, dateFilter) {
function link(scope, element, attrs) {
var format,
timeoutId;
function updateTime() {
element.text(dateFilter(new Date(), format));
}
scope.$watch(attrs.myCurrentTime, function (value) {
format = value;
updateTime();
});
element.on('$destroy', function () {
$interval.cancel(timeoutId);
});
// start the UI update process; save the timeoutId for canceling
timeoutId = $interval(function () {
updateTime(); // update DOM
}, 1000);
}
return {
link: link
};
};
// Added by ng-annotate
bd.myCurrentTime.Directive.factory.$inject = ["$interval", "dateFilter"];
html 文件:
<div ng-controller="Controller">
Date format: <input ng-model="format"> <hr />
Current time is: <span my-current-time="format"></span>
</div>
您使用的是 Closure 编译器的高级编译模式吗?没有看到你的缩小代码很难查明问题,但这里有一些想法:
在这部分代码中:
.controller('Controller', ['$scope', function ($scope) { $scope.format = 'M/d/yy h:mm:ss a'; }])
$scope.format
可能会重命名为类似s.f
的名称。通常,这不是问题,特别是因为$scope
被正确注入。但是,您在 HTML 中引用了format
属性,Closure 编译器不知道:Date format: <input ng-model="format"> <hr /> Current time is: <span my-current-time="format"></span>
尝试改用
$scope['format'] = 'M/d/yy h:mm:ss a'
- Closure 编译器从不更改字符串,因此这应该为 HTML 导出正确的 属性 名称。现在,记住 - once you use a string to access a property, always use a string to access that property.与#1 类似,
attrs.myCurrentTime
可能会重命名为a.m
。尝试使用字符串 属性 名称 (attrs['myCurrentTime']
) 以确保编译的 JavaScript 正确匹配 HTML。
同样,这些只是此时的想法。如果您 post 将缩小的代码作为完整示例,我们将能够提供更多帮助。