简体   繁体   English

AngularJS自定义指令在继承父作用域时访问模板中的属性

[英]AngularJS Custom Directive Access attributes in Template while inheriting parent scope

I have a custom directive that I would like to inherit the parent scope. 我有一个自定义指令,我想继承父作用域。 I would also like to pass a value via an attribute. 我还想通过属性传递一个值。 It looks like this: 它看起来像这样:

Controller 调节器

app.controller('Main', ['$scope', function($scope){
   $scope.cols = { 'col1': true, 'col2':false, 'col3': true};

   $scope.toggleCol = function(colName){
       $scope.cols[colName] = !$scope.cols[colName];
   };
}]);

Directive 指示

wrApp.directive("custTh", function(){
 return {
    restrict: "EA",
    scope: false,
    replace: true,
    template: '<th ng-show="cols[{{ colname }}]" ng-click="toggleCol({{ colname }})">{{ colname }}</th>',

 };
});

HTML HTML

<th cust-th colname="col2"></th>

I just can't seem to get access to the attribute value bc I am inheriting the parent scope. 我似乎无法访问属性值bc我继承父范围。 Is it possible to directly access directive attributes from the template? 是否可以直接从模板访问指令属性?

As long as you don't declare an isolate scope in the directive, you can access the parent scope: 只要您不在指令中声明隔离范围,就可以访问父范围:

<-- cust-th template can access Main controller's scope -->
<div ng-controller="Main">
  <th cust-th></th>
</div>

There are just a few syntax errors in your code that are preventing you from doing this. 您的代码中只有一些语法错误阻止您执行此操作。 In your template, you do not need to interpolate the values being passed into the angular directives (ng-click and ng-show): 在模板中,您不需要插入传递给angular指令的值(ng-click和ng-show):

<th ng-show="cols[colname]" ng-click="toggleCol(colname)">{{ colname }}</th>   

colname has not been declared as a scope variable in your controller, so when Angular goes to compile your HTML, it will not recognize it. colname尚未在控制器中声明为范围变量,因此当Angular编译HTML时,它将无法识别它。 If you would like to continue passing it as an attribute on your HTML element, you'll need to create an angular element in your directive in order to access the value using a link function (See https://docs.angularjs.org/guide/directive ). 如果您希望继续将其作为HTML元素的属性传递,则需要在指令中创建一个angular元素,以便使用链接函数访问该值(请参阅https://docs.angularjs.org/指导/指示 )。 If you want to use interpolation ( {{colname}} ), then you will need to have a variable in your controller like 如果你想使用插值({{colname}}),那么你需要在你的控制器中有一个变量

$scope.colname = 'col1';

No. You can't access directive attributes while you inherit parent scope. 否。继承父作用域时,无法访问指令属性。 To do this you have to create directive's own scope like below: 要做到这一点,你必须创建指令自己的范围,如下所示:

app.directive("custTh", function(){
      return {
      restrict: "EA",
      scope: { colname: '@'},   
      template: 'Name : {{ colname }}',
    };
});

and in yout HTML remplate you have to write like this: 在你的HTML remplate你必须这样写:

<div ng-controller="Main">   
   <cust-th colname="col2" ></cust-th>
</div>

I have also created a fiddle for you help. 我也为你提供了帮助。 I you find this useful then please accept this as an answer. 我觉得这很有用,请接受这个作为答案。

http://jsfiddle.net/HB7LU/10806/ http://jsfiddle.net/HB7LU/10806/

You can inherit the scope but also create a child scope by using: 您可以继承范围,但也可以使用以下命令创建子范围:

scope: true

Then in order to pass values: 然后为了传递值:

link: function(scope, element, attrs) {
     scope.$watch(attrs.valueFromOutside, function(newValue) {
         scope.valueFromOutside = newValue;
     });
 }

This way in the inner scope of the directive you can have a different value while still having access to the parent scope. 这种方式在指令的内部范围内,您可以拥有不同的值,同时仍然可以访问父作用域。

You can see it all in action here: http://jsfiddle.net/HB7LU/15120/ 你可以在这里看到一切: http//jsfiddle.net/HB7LU/15120/

A template can be a string or a function: 模板可以是字符串或函数:

template 模板

HTML markup that may: 可能的HTML标记:

Replace the contents of the directive's element (default). 替换指令元素的内容(默认)。 Replace the directive's element itself (if replace is true - DEPRECATED). 替换指令的元素本身(如果replace为true - DEPRECATED)。 Wrap the contents of the directive's element (if transclude is true). 包装指令元素的内容(如果transclude为true)。 Value may be: 价值可能是:

A string. 一个字符串。 For example <div red-on-hover>{{delete_str}}</div> . 例如<div red-on-hover>{{delete_str}}</div> A function which takes two arguments tElement and tAttrs (described in the compile function api below) and returns a string value. 一个函数,它接受两个参数tElement和tAttrs(在下面的编译函数api中描述)并返回一个字符串值。

See example at use attribute in template 请参阅模板use属性的示例

See document at Angular Doc 请参阅Angular Doc上的文档

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

相关问题 如何在保持对父范围的访问的同时从自定义指令访问元素属性? - How to access elements attributes from custom directive while keeping access to parent scope? 以功能为模板访问重复AngularJS指令中的`$ scope` - Access `$scope` in Repeating AngularJS Directive with Function as Template 使用模板在angular指令中访问父范围 - Access parent scope in angular directive with template 如何在自定义属性指令中访问父范围 - How to access parent scope in custom attribute directive Angularjs自定义指令范围:true更改它的父范围 - Angularjs custom directive scope:true change it's parent scope 如何在子指令模板中访问父指令属性 - How to access parent directive attributes inside the child directive template AngularJs指令:从模板中的父作用域调用方法 - AngularJs directive: call method from parent scope within template 没有模板的AngularJS指令,并且从父范围共享更新 - AngularJS Directive with no Template and Sharing the updates from parent scope 如何将指令模板的属性附加到AngularJS中父元素以外的其他元素? - How to attach attributes of a directive template to other elements than the parent in AngularJS? Angularjs - 如何通过指令中的表达式访问父作用域的变量 - Angularjs - how to access parent scope's variable by expression from a directive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM