简体   繁体   English

如何在指令的控制器中访问父级控制器的范围?

[英]How to access the scope of a parent's controller's in a directive's controller?

I have a custom directive nested in a parent div who's controller sets a variable value to its scope, such as: 我有一个嵌套在父div中的自定义指令,该父div的控制器将变量设置为其范围,例如:

html HTML

<div ng-controller="mainCtrl">
    <p>{{data}}</p>
    <myDirective oncolour="green" offcolour="red" data="data"></myDirective>
</div>

javascript JavaScript的

app.controller("mainCtrl", function($scope){
   $scope.data= 1;
});

app.directive("myDirective", function() {
   return {
       restrict: 'E',
       scope: {
           oncolour: '@',
           offcolour: '@',
           data: '='
       },
       template: '<p>{{data}} is {{state}}</p>',
       controller: function($scope){

           console.log($scope.data); //undefined!

          /* logic to set $scope.state depending on $scope.data */

       };
   };
});

I can pass the value of data to the directive as I can see the {{data}} off the template being parsed correctly. 我可以将data的值传递给指令,因为可以看到正确解析的模板中的{{data}}

However, $scope.data is undefined in the directive's controller. 但是, $ scope.data在指令的控制器中未定义。 I need to apply some logic there depending on this variable like if data==1, then state="on", else state="off" , so I can then apply the oncolour and offcolour appropriately. 我需要根据此变量在其中应用一些逻辑,例如, 如果data == 1,则为state =“ on”,否则为state =“ off” ,因此我可以适当地应用上色和下

Any idea how this can be achieved? 知道如何实现吗?

"transclude" options is necessary: “ transclude”选项是必需的:

   transclude: true,
   controller: ['$scope', function($scope) {
       console.log($scope.data);
   }],

You miss the $ before the scope in your directive. 您在指令中的作用域之前错过了$ The reason you will have undefined is because in the directive $scope is not the same as return scope . 之所以未定义,是因为指令$scope与return scope You have to match them. 您必须匹配它们。

app.directive("myDirective", function() {
  return {
    restrict: 'E',
    $scope: {
       oncolour: '@',
       offcolour: '@',
       data: '='
    },
    template: '<p>{{data}} is {{state}}</p>',
    controller: function($scope){

       console.log($scope.data); //undefined!

      /* logic to set $scope.state depending on $scope.data */

    };
   };
});

change $scope to scope , take a look to angular directive documentation , goto end of page and check sample code 改变$scopescope ,看看对角指令文档 ,页面转到结束,并检查示例代码

app.directive("myDirective", function() {
   return {
       restrict: 'E',
       scope: {
           oncolour: '@',
           offcolour: '@',
           data: '='
       },
       template: '<p>{{data}} is {{state}}</p>',
       controller: function(scope){

           console.log(scope.data); //undefined!

          /* logic to set $scope.state depending on $scope.data */

       };
   };
});

My bet is that the binding is failing as data is a primitive, see https://github.com/angular/angular.js/wiki/Understanding-Scopes 我敢打赌,绑定失败,因为数据是原始数据,请参阅https://github.com/angular/angular.js/wiki/Understanding-Scopes

Try and change: 尝试更改:

app.controller("mainCtrl", function($scope){
   $scope.data= 1;
});

And

<div ng-controller="mainCtrl">
    <p>{{data}}</p>
    <myDirective oncolour="green" offcolour="red" data="data"></myDirective>
</div>

Into: 成:

app.controller("mainCtrl", function($scope){
   $scope.data= {
      value: 1
   };
}); 

And

<div ng-controller="mainCtrl">
    <p>{{data.value}}</p>
    <myDirective oncolour="green" offcolour="red" data="data.value"></myDirective>
</div>

Check AngluarJS normalization . 检查AngluarJS 规范化

The directive elements name is wrong: 指令元素名称错误:

<myDirective ... >

It should be 它应该是

<my-directive ...>

And make sure to access scope instead of $scope. 并确保访问作用域而不是$ scope。

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

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