我想我只是在做一些愚蠢的事情,但我想訪問一個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/>");
});
}
這似乎應該是很簡單但我無法弄清楚我做錯了什麼傻事。謝謝!
是的,並再次感謝。我應該知道它必須是蛇的情況下,因爲一切都是:)。 –