gpt4 book ai didi

仅当条件为 true 时才使用 AngularJS 指令

转载 作者:行者123 更新时间:2023-12-02 10:21:29 27 4
gpt4 key购买 nike

我将在 ng-repeat 项目中有一个上下文菜单指令。根据条件是否为真,应应用该指令。如何设置仅当 item.hasMenu == true 时的条件然后应用指令?

<ul ng-controller="ListViewCtrl" >
<li contextmenu ng-repeat="item in items">{{item.name}} </li>
</ul>

编辑

这似乎对我有用。首先是指令。

app.directive('menu',function(){

return {
restrict : 'A',

link : function(scope,element,attrs){

if(scope.hasMenu){
element.contextmenu({
menu:[
{title:"Remove" , "cmd" : "remove"},
{title:"Add" , "cmd" : "add"},
],
select:function(event,ui){
//alert("select " + ui.cmd + " on" + ui.target.text());
if (ui.cmd ==='remove'){
alert('Remove selected on ' + scope.item);
}
if (ui.cmd ==='add'){
alert("Add selected");
}
}
});
}

}
}
}
);

然后是html

 <ul ng-controller="ListViewCtrl" >
<li menu ng-repeat="item in items">{{item.name}} </li>
</ul>

最佳答案

你能使用ng-if做这样的事情吗?

<ul ng-controller="ListViewCtrl" >
<li ng-repeat="item in items">
<span>{{item.name}}</span>
<div contextmenu ng-if="item.hasMenu"></div>
</li>
</ul>

Here are the docs for ng-if.

编辑:如果您要从类中驱动上下文菜单,您应该能够执行以下操作:

<ul ng-controller="ListViewCtrl" >
<li ng-class="{'hasmenu': item.hasMenu}" ng-repeat="item in items">{{item.name}} </li>
</ul>

关于仅当条件为 true 时才使用 AngularJS 指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18382331/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com