ng-show在angularjs中无法正常工作

时间:2015-07-14 10:11:25

标签: angularjs

我想根据控制器内属性的值显示元素。

eReaderBook.directive("controlTools", function () {
return {
    replace : true,
    restrict : "E",
    templateUrl : "assets/directives/controlstools.html",
    controller : function ($scope) {
        $scope.visible = false;
    },
    link : function (scope, el, attr) {

       el.bind("mouseover",function()
        {
           console.log(scope.visible)
           scope.visible = true;
        });
        el.bind("mouseout",function()
        {
            console.log(scope.visible)
             scope.visible = false;
        })
    }
};

});

<div class="menuitem"><span class="glyphicon glyphicon-search"></span><span ng-show="visible" class="menutext">Toc</span></div>

在指令中,我有一个用$scope.visible = false定义的控制器。页面加载时一切正常。我想将状态更改为$scope.visible = true上的mouseover$scope.visible = false上的mouseout。为什么scope.visible = true;不会影响ng-show

1 个答案:

答案 0 :(得分:0)

You just forgot to add scope: true that is responsible to share scope.

return {
  replace: true,
  restrict: "E",
  scope: true,
  templateUrl: "assets/directives/controlstools.html",