升级到 1.4 后,我的指令标签模型不再有效
After upgrading to 1.4, my directive tag model doesn't work anymore
最近我们升级到 1.4,现在我对悬停指令的工作方式有疑问。
以前的工作代码:
angular
.module('tagHoverDirective', []).controller('TagsHover', TagsHover)
.directive('tagsHover', directive);
function directive () {
var directive = {
templateUrl : "popovers/tagsPopovers/tagsHover.html",
restrict: "E",
replace: true,
bindToController: true,
controller: TagsHover
link: link,
scope: {
tag:'=ngModel'
}
};
return directive;
function link(scope, element, attrs) {}
}
TagsHover.$inject = [
'$scope',
'$timeout'];
function TagsHover(
$scope,
$timeout) {
....
现在 1.4 强制我们使用 Controller as syntax
,我不得不将 function directive ()
部分更改为:
function directive () {
var directive = {
templateUrl : "popovers/tagsPopovers/tagsHover.html",
restrict: "E",
replace: true,
bindToController: true,
controller: 'TagsHover as tgh',
link: link,
scope: {
tag:'=ngModel'
}
};
return directive;
function link(scope, element, attrs) {}
}
现在标记中的 ng-show
不再有效,但我该如何修复它?我没有使用 tgh
或 this
,我实际上是在使用传递给该指令的 tag
对象来设置可见性。
<div class="tags-hover-container" ng-show="tag.tagsHoverDisplay">
实际上,当您将 "as" 部分绑定到作用域时公开控制器。
所以 $scope.anything 变成 "as".anything.
您正在将控制器公开为
controller: 'TagsHover as tgh',
这意味着您将 $scope/this 绑定到对象 tgh。所以 scope.tag 变成 tgh.tag
您的模板现在应该是
<div class="tags-hover-container" ng-show="tgh.tag.tagsHoverDisplay">
最近我们升级到 1.4,现在我对悬停指令的工作方式有疑问。
以前的工作代码:
angular
.module('tagHoverDirective', []).controller('TagsHover', TagsHover)
.directive('tagsHover', directive);
function directive () {
var directive = {
templateUrl : "popovers/tagsPopovers/tagsHover.html",
restrict: "E",
replace: true,
bindToController: true,
controller: TagsHover
link: link,
scope: {
tag:'=ngModel'
}
};
return directive;
function link(scope, element, attrs) {}
}
TagsHover.$inject = [
'$scope',
'$timeout'];
function TagsHover(
$scope,
$timeout) {
....
现在 1.4 强制我们使用 Controller as syntax
,我不得不将 function directive ()
部分更改为:
function directive () {
var directive = {
templateUrl : "popovers/tagsPopovers/tagsHover.html",
restrict: "E",
replace: true,
bindToController: true,
controller: 'TagsHover as tgh',
link: link,
scope: {
tag:'=ngModel'
}
};
return directive;
function link(scope, element, attrs) {}
}
现在标记中的 ng-show
不再有效,但我该如何修复它?我没有使用 tgh
或 this
,我实际上是在使用传递给该指令的 tag
对象来设置可见性。
<div class="tags-hover-container" ng-show="tag.tagsHoverDisplay">
实际上,当您将 "as" 部分绑定到作用域时公开控制器。
所以 $scope.anything 变成 "as".anything.
您正在将控制器公开为
controller: 'TagsHover as tgh',
这意味着您将 $scope/this 绑定到对象 tgh。所以 scope.tag 变成 tgh.tag
您的模板现在应该是
<div class="tags-hover-container" ng-show="tgh.tag.tagsHoverDisplay">