本質上我想能夠訪問我創建的指令的父範圍,但我也希望能夠訪問已放置到元素上的屬性。如何從自定義指令中訪問元素屬性,同時保持對父範圍的訪問?
例如有關JS
app.directive('testDirective', function(){
return {
restrict:"E",
templateUrl:"directive.html",
scope:{
testAttribute: '='
}
};
});
app.controller('mainCtrl', function($scope){
$scope.name = 'henry'
}
的index.html
<div ng-controller="mainCtrl">
<test-directive test-attribute="Hello"></test-directive>
</div>
directive.html
{{testAttribute}}
{{name}}
輸出爲 「你好」,而不是 「你好亨利」
所以只是爲了clar rify,我希望能夠做的就是訪問屬性和父範圍。
在您的directive.html中,您可以使用'{{$ parent.name}}'來訪問屬性'name'。但是它不合適,因爲它不會促進可重用性。 – ryeballar 2014-10-05 02:37:07