Я хочу получить $ 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 Действительно, благодаря всем вам. особенно для u @immirza
им так жаль, что я не могу ответить u один за другим. это просто добавить $parent
//how to access that 'something'
var bah = scope.$parent.something
он должен быть доступен в вашей директиве, так как он не имеет изолированной области. –
console.log (scope), чтобы получить лучшее понимание. –