单击事件未在 ng-repeat 中的属性指令中触发
click event not firing in Attribute directive inside ng-repeat
<tr ng-repeat="school in schools">
<td>{{$index+1}}</td>
<td>
<span school-edit>{{school.name}}</span>
</td>
</tr>
我的指令
app.directive("school-edit",function()
{
return {
restrict : "A",
link : function(scope,el,atr)
{
el.on("click",function()
{
alert("called");
});
}
};
});
点击事件没有触发
我想指令是在 ng-repeat 填充 table
之前注册的
正在工作的 plinker http://plnkr.co/edit/XARGhQkZHgOcGZvyiD33?p=preview
OP,您的命名约定似乎不正确。定义指令时避免使用破折号。在 js 中选择驼峰式 .. myDirective,然后在 html 中调用为 my-directive。
定义一个指令,这可以通过使用我定义的示例来实现。
app.directive('school', [function() {
return {
restrict: 'A',
link: function(scope, elem, attrs) {
elem.bind('click', function() {
alert("g");
});
}
}
}]);
这将为您完成。
<div school>click me!</div>
你要找的是onclick
app.directive("school-edit",function()
{
return {
restrict:"A",
link:function(scope,el,atr)
{
el.onclick = function(){
alert("called");
};
}
};
});
如果有人仍在寻找答案,以下是正确的方法(注意命名约定的差异):
app.directive("schoolEdit",function()
{
return {
restrict: "A",
link : function(scope,el,atr)
{
el.on("click",function()
{
alert("called");
});
}
};
});
<tr ng-repeat="school in schools">
<td>{{$index+1}}</td>
<td>
<span school-edit>{{school.name}}</span>
</td>
</tr>
我的指令
app.directive("school-edit",function()
{
return {
restrict : "A",
link : function(scope,el,atr)
{
el.on("click",function()
{
alert("called");
});
}
};
});
点击事件没有触发 我想指令是在 ng-repeat 填充 table
之前注册的正在工作的 plinker http://plnkr.co/edit/XARGhQkZHgOcGZvyiD33?p=preview
OP,您的命名约定似乎不正确。定义指令时避免使用破折号。在 js 中选择驼峰式 .. myDirective,然后在 html 中调用为 my-directive。
定义一个指令,这可以通过使用我定义的示例来实现。
app.directive('school', [function() {
return {
restrict: 'A',
link: function(scope, elem, attrs) {
elem.bind('click', function() {
alert("g");
});
}
}
}]);
这将为您完成。
<div school>click me!</div>
你要找的是onclick
app.directive("school-edit",function()
{
return {
restrict:"A",
link:function(scope,el,atr)
{
el.onclick = function(){
alert("called");
};
}
};
});
如果有人仍在寻找答案,以下是正确的方法(注意命名约定的差异):
app.directive("schoolEdit",function()
{
return {
restrict: "A",
link : function(scope,el,atr)
{
el.on("click",function()
{
alert("called");
});
}
};
});