我想從我的指令鏈接訪問控制器中的$ scope。它失敗並拋出一個錯誤。謝謝你的幫助。
錯誤是type error: scope.something is undefined
。
HTML
<div ng-controller="myController">
show me something {{ something }}
<!-- this is a canvas -->
<my-directive></my-directive>
</div>
JS
angular.module('fooBar',[]).controller(myController, ['$scope', function($scope) {
// this is the something
$scope.something = false;
}]).directive(myDirective, function(){
return {
template: '<canvas width='500px' height='500px'></canvas>',
link: function(scope, el, attr) {
//how to access that 'something'
var bah = scope.something;
}
};
});
UPDATE 真的感謝你們。尤其是對你來說@immirza
即時對不起,我不能一一答覆你。 它只是添加$parent
//how to access that 'something'
var bah = scope.$parent.something
它應該在您的指令中可用,因爲它沒有隔離範圍。 –
console.log(範圍)以便更好地理解 –