我想我只是在做一些愚蠢的事情,但我想访问一个HTML元素的自定义属性,我附加了一个指令,但它不起作用。我见过其他人使用范围。$ eval,但我似乎无法得到这个工作。无法从Angularjs指令访问属性
这里就是我想(的jsfiddle这里:https://jsfiddle.net/u5d3gfny/1/):
<!DOCTYPE html>
<html ng-app="AngAttrTest">
<head>
<title>Bootstrap 3</title>
</head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<body>
<a href="#" id="my-modal" class="btn btn-success fcvt-btn-save" nextModalId="fileFormatModal" chain-modal>Continue</a>
<div id="appendMe"/>
<script>
var app = angular.module('AngAttrTest', []);
app.directive('chainModal', ['$document', function($document){
return {
link: function(scope, elem, attrs) {
elem.bind('click', function() {
$('#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: " + attrs.nextModalId + "<br/><br/>");
});
}
}
}]);
</script>
</body>
</html>
我也试图与该更换我的链接功能没有avial:
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/>");
});
}
这似乎应该是很简单但我无法弄清楚我做错了什么傻事。谢谢!
是的,并再次感谢。我应该知道它必须是蛇的情况下,因为一切都是:)。 –