Jquery 无法从Angularjs指令访问属性
我想我只是在做一些愚蠢的事情,但我想访问一个HTML元素上的自定义属性,我附加了一个指令,但它不起作用。我见过其他人使用scope.$eval,但我似乎也无法让它发挥作用 以下是我尝试过的(这里是jsfiddle:):Jquery 无法从Angularjs指令访问属性,jquery,angularjs,Jquery,Angularjs,我想我只是在做一些愚蠢的事情,但我想访问一个HTML元素上的自定义属性,我附加了一个指令,但它不起作用。我见过其他人使用scope.$eval,但我似乎也无法让它发挥作用 以下是我尝试过的(这里是jsfiddle:): 引导程序3 var app=angular.module('AngAttrTest',[]); 应用程序指令('chainModal',['$document',函数($document){ 返回{ 链接:功能(范围、要素、属性){ 元素绑定('单击',函数(){ $(“#ap
引导程序3
var app=angular.module('AngAttrTest',[]);
应用程序指令('chainModal',['$document',函数($document){
返回{
链接:功能(范围、要素、属性){
元素绑定('单击',函数(){
$(“#appendMe”).append(“这是有效的,ID:+attrs.ID+”
);
$(“#appendMe”).append(“类:+attrs.Class+”
)也是如此;
$('#appendMe').append(“但是我的自定义属性nextModalId出现时未定义:“+attrs.nextModalId+”
”);
});
}
}
}]);
我还尝试用以下内容替换我的链接功能:
link: function(scope, elem, attrs) {
elem.bind('click', function() {
var nextModalId = scope.$eval(attrs.nextModalId);
$('#appendMe').append("This works, ID: " + attrs.id + "<br/><br/>");
$('#appendMe').append("So does this, Class: " + attrs.class + "<br/><br/>");
$('#appendMe').append("But my custom Attribute nextModalId comes up undefined: " + nextModalId + "<br/><br/>");
});
}
链接:功能(范围、元素、属性){
元素绑定('单击',函数(){
var nextModalId=范围$eval(attrs.nextModalId);
$(“#appendMe”).append(“这是有效的,ID:+attrs.ID+”
);
$(“#appendMe”).append(“类:+attrs.Class+”
)也是如此;
$('#appendMe').append(“但是我的自定义属性nextModalId出现时未定义:“+nextModalId+”
”);
});
}
这看起来应该很简单,但我不知道我做错了什么蠢事。谢谢 当指令用作属性时,必须在snake case中声明。修复 更改下一个模式:
<a href="#"
id="my-modal"
class="btn btn-success fcvt-btn-save"
nextModalId="fileFormatModal"
chain-modal>
Continue
</a>
问题在于elem.bind函数,而不是指令。我这样更改了代码:
var app = angular.module('AngAttrTest', []);
app.directive('chainModal', ['$document', function($document){
return {
link: function(scope, elem, attrs) {
elem.bind('click', { id: attrs['id'], class: attrs['class'], nextModalId: attrs['nextModalId'] }, function(event) {
$('#appendMe').append("This works, ID: " + event.data.id + "<br/><br/>");
$('#appendMe').append("So does this, Class: " + event.data.class + "<br/><br/>");
$('#appendMe').append("But my custom Attribute nextModalId comes up undefined: " + event.data.nextModalId + "<br/><br/>");
});
}
}
}]);
var-app=angular.module('AngAttrTest',[]);
应用程序指令('chainModal',['$document',函数($document){
返回{
链接:功能(范围、要素、属性){
元素绑定('click',{id:attrs['id'],class:attrs['class'],nextModalId:attrs['nextModalId']},函数(事件){
$(“#appendMe”).append(“这很有效,ID:+event.data.ID+”
);
$('#appendMe').append(“类:“+event.data.Class+””
)也是如此;
$('#appendMe').append(“但是我的自定义属性nextModalId出现时未定义:“+event.data.nextModalId+””
);
});
}
}
}]);
是的,再次感谢。我应该知道这一定是蛇的案子,因为其他一切都是:)。
<a href="#"
id="my-modal"
class="btn btn-success fcvt-btn-save"
next-modal-id="fileFormatModal"
chain-modal>
Continue
</a>
var app = angular.module('AngAttrTest', []);
app.directive('chainModal', ['$document', function($document){
return {
link: function(scope, elem, attrs) {
elem.bind('click', { id: attrs['id'], class: attrs['class'], nextModalId: attrs['nextModalId'] }, function(event) {
$('#appendMe').append("This works, ID: " + event.data.id + "<br/><br/>");
$('#appendMe').append("So does this, Class: " + event.data.class + "<br/><br/>");
$('#appendMe').append("But my custom Attribute nextModalId comes up undefined: " + event.data.nextModalId + "<br/><br/>");
});
}
}
}]);