javascript - 为什么我不能在 AngularJS 中使用 ng-show inside 指令?

标签 javascript angularjs

我正在尝试制作一个在单击它时显示其内容的元素: 有很多方法可以做到这一点。我想在指令内执行此操作。 为什么不起作用?

http://plnkr.co/edit/Y13bcD7Ikuk6pERM9Mm8?p=preview

HTML:

<ez-option title="option 1">
        Option 1 inside
        <input ng-model="x">{{x}}
    </ez-option>
    <ez-option title="option 2">
        Option 2 inside
   </ez-option>

JavaScript:

directive('ezOption',function(){
    return {
        template:'<li></li><div ng-show=selected ng-transclude class=body></div>',
        link:function ($scope,iElement,iAttrs,controller,tanscludeFn){
            iElement.find('li').html(iAttrs.title)
            iElement.find('li').on('click',function(){
              $scope.selected=true
            })
        },
        scope:true,
        transclude:true
    }
})

最佳答案

这是因为 Angular 不知道您的点击事件的范围变化。为了让它意识到,你应该这样做

            iElement.find('li').on('click',function(){
              $scope.$apply(function() {
                $scope.selected=true;
              })
            })

或者,您可以使用 Angular 的 ng-click 代替

m.directive('ezOption',function(){
    return {
        template:'<li ng-click="selected=true"></li><div ng-show=selected ng-transclude class=body></div>',
        link:function ($scope,iElement,iAttrs,controller,tanscludeFn){
            iElement.find('li').html(iAttrs.title);
        },
        controller:function($scope){
          $scope.selected=false
        },
        transclude:true
    }
})

关于javascript - 为什么我不能在 AngularJS 中使用 ng-show inside 指令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31551631/

相关文章:

javascript - 在新行上拆分,除非在双引号内

javascript - 如何将 jqplot yaxis 格式化为整数

javascript - 在单击窗口之前未检测到 keydown

javascript - 读取当前文本输入值后进行 Ajax 调用

angularjs - 我如何使用 Angular js在另一个html文件中使用从json响应获取的数据

javascript - 如何防止在javascript中按退格键时光标返回?

javascript - 不确定为什么 'userApp' 模块不可用

javascript - Angular - 异步数据流问题

angularjs - 如何使用 AngularJS 对结果进行分页但在完整列表中进行过滤?

javascript - Angular Controller 的动态属性