升级到1.4后,我的指令标签模型不再起作用了

时间:2015-06-29 19:00:47

标签: angularjs angularjs-directive angularjs-ng-model angularjs-ng-show

最近我们升级到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不再有效,但我该如何解决?我没有使用tghthis,我实际上正在使用传递给此指令的tag对象来设置可见性。

<div class="tags-hover-container" ng-show="tag.tagsHoverDisplay">

1 个答案:

答案 0 :(得分:2)

实际上,当您公开控制器时,您将“作为”范围绑定到范围。

所以$ scope.anything变成“as”。一切。

您正在将控制器公开为

controller: 'TagsHover as tgh',

这意味着你将$ scope / this绑定到对象tgh。因此,scope.tag成为tgh.tag

您的模板现在应该是

<div class="tags-hover-container" ng-show="tgh.tag.tagsHoverDisplay">