简体   繁体   English

如何在指令链接中访问angular $ scope控制器?

[英]How to access angular $scope controller in directive link?

I want to access $scope in controller from my directive link. 我想从我的指令链接访问控制器中的$ scope It failed and threw an error. 它失败并抛出错误。 Thanks for your help. 谢谢你的帮助。

The error is type error: scope.something is undefined . 错误是type error: scope.something is undefined

HTML 的HTML

<div ng-controller="myController">
  show me something {{ something }}
  <!-- this is a canvas -->
  <my-directive></my-directive>
</div>

JS 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 Really thanks to you all. 更新真的谢谢大家。 especially to u @immirza 特别是对你@immirza

im so sorry i cant reply u one by one. 我很抱歉,我不能一一答复你。 it just add $parent 它只是添加$parent

//how to access that 'something'
var bah = scope.$parent.something

Did you try to set the "scope" option to "false" in the directive declaration? 您是否尝试在指令声明中将“ scope”选项设置为“ false”? With this option, the scope of the directive must be the same as the controller's scope. 使用此选项时,伪指令的范围必须与控制器的范围相同。

change to this... 改成这个...

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;//changed from scope.something to $scope.something 
    }
  };
});

If I understand your question correctly you want to have a directive that: 如果我正确理解了您的问题,那么您希望使用以下指令:

1/ Does not have an isolated scope (so you can access parent scope and interact, with it) 1 /没有独立的作用域(因此您可以访问父作用域并与其进行交互)

2/ You want to share some scope properties with your controller. 2 /您想与控制器共享一些范围属性。

So solution is: 所以解决方案是:

1/ @jkoestinger answer, 1 / @jkoestinger回答,

2/ using directive attributes and scope options object. 2 /使用指令属性和范围选项对象。

But for me you should spend some time here: https://docs.angularjs.org/guide/directive 但是对我来说,您应该在这里花一些时间: https : //docs.angularjs.org/guide/directive

Your code will work as you are expecting. 您的代码将按预期工作。 This problem is some type errors are there in your code. 此问题是您的代码中存在一些类型错误。

Please use 请用

HTML 的HTML

<div ng-controller="myController">
      show me something {{ something }}
      <!-- this is a canvas -->
      <my-directive></my-directive>
</div>

Angular 角度的

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;
        console.log(bah)
      }
    };
  });

Please refer fiddle 请参考小提琴

Explaination 讲解

The directive has no scope of itself unless specified. 除非指定,否则该伪指令本身没有范围。 It will have scope to that of the parent. 它将具有父项的范围。 In This case ' scope of myDirective ' is same as scope of myController . 在这种情况下,“ scope of myDirectivescope of myDirectivescope of myController相同。 This is because we can access scope.something in directive scope . 这是因为我们可以访问scope.something in directive scope

Error in your code 代码错误

  1. Directive and controller name should be in quotes. 指令和控制器名称应用引号引起来。 ie( 'myDirective' not myDirective ) 即( 'myDirective'不是myDirective
  2. Quotes inside quotes should be seperated. 引号内的引号应分开。

    '<canvas width="500px" height="500px"></canvas>' //correct '<canvas width="500px" height="500px"></canvas>' //correct

     `'<canvas width='500px' height='500px'></canvas>'` `//incorrect` 

scope.something is undefined because the directives linking function is called before the controller is invoked. scope.something是未定义的,因为在调用控制器之前已调用了指令链接功能。 You need to use $watch . 您需要使用$watch

angular.module('fooBar',[])
   .controller('myController', ['$scope', function($scope) {
       // this is the something
       $scope.something = false;
}]);


angular.module('fooBar').directive('myDirective', function(){
  return {
    template: '<canvas width='500px' height='500px'></canvas>',
    link: function(scope, el, attr) {
        var blah;
        scope.$watch("something", function (value) {
            blah = value;
        });
     }
  };
});

FYI, the scope is parent to directive so you need access as parent scope. 仅供参考, scope is parent to directive因此您需要作为父级范围进行访问。 Try following and it should work. 尝试遵循,它应该可以工作。

Also, the best way to know why it throw not defined error... put a breakpoint to the line, mouse over to $scope and and see all available items to scope. 另外,了解为什么会引发未定义错误的最好方法是...在该行上放置一个断点,将鼠标悬停在$ scope上,然后查看所有可用范围的项目。

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.$parent.something; // added parent.
    }
  };
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM