简体   繁体   English

使用控制器作为语法时,如何从父指令继承属性?

[英]How do I inherit properties from parent directive when using the controller as syntax?

I want to use the Controller As syntax in my Angular directives for two reasons. 由于两个原因,我想在Angular指令中使用Controller As语法。 It's more plain JS and there's no dependency on the $scope service which will not be available in Angular 2.0. 它是更普通的JS,并且没有对$ scope服务的依赖,Angular 2.0中将不提供该服务。

It works great for a single directive but I cannot figure out how to print a property from the controller of a parent directive in a child directive. 它适用于单个指令,但是我无法弄清楚如何从子指令的父指令的控制器中打印属性。

function parentCtrl () {
  this.greeting = { hello: 'world' };
}

function childCtrl () {}


angular.module('app', [])
  .controller('parentCtrl', parentCtrl)
  .controller('childCtrl', childCtrl)
  .directive('myParent', function () {
    return {
      scope: {},
      bindToController: true,
      controller: 'parentCtrl',
      controllerAs: 'parent',
      template: '<my-child></my-child>'
    }
  })
  .directive('myChild', function () {
    return {
      scope: {
        greeting: '='
      },
      bindToController: true,
      controller: 'childCtrl',
      controllerAs: 'child',
      template: '<p>{{ greeting.hello }}</p>'
    }
  });

You have to require the parent controller, the use the link function to inject the parent to the child. 您必须require父级控制器,使用link功能才能将父级注入子级。 The myChild directive would become: myChild指令将变为:

.directive('myChild', function () {
    return {
        scope: {
            // greeting: '=' // NO NEED FOR THIS; USED FROM PARENT
        },
        bindToController: true, // UNNECESSARY HERE, THERE ARE NO SCOPE PROPS
        controller: 'childCtrl',
        controllerAs: 'child',
        template: '<p>{{ child.greeting.hello }}</p>', // PREFIX WITH VALUE
                                                       // OF `controllerAs`
        require: ['myChild', '^myParent'],
        link: function(scope, elem, attrs, ctrls) {
            var myChild = ctrls[0], myParent = ctrls[1];
            myChild.greeting = myParent.greeting;
        }
    }
});

I found that you can use element attributes to pass properties from the parent directive controller's scope to a child. 我发现您可以使用元素属性将属性从父指令控制器的范围传递给子级。

function parentCtrl () {
  this.greeting = 'Hello world!';
}

function myParentDirective () {
  return {
    scope: {},
    controller: 'parentCtrl',
    controllerAs: 'ctrl',
    template: '<my-child greeting="ctrl.greeting"></my-child>'
  }
}

function childCtrl () {}

function myChildDirective () {
  return {
    scope: {
      greeting: '='
    },
    bindToController: true,
    controller: 'childCtrl',
    controllerAs: 'ctrl',
    template: '<p>{{ ctrl.greeting }}</p><input ng-model="ctrl.greeting" />'
  }
}

angular.module('parent', [])
  .controller('parentCtrl', parentCtrl)
  .directive('myParent', myParentDirective);

angular.module('child', [])
  .controller('childCtrl', childCtrl)
  .directive('myChild', myChildDirective);

angular.module('app', ['parent', 'child']);

暂无
暂无

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

相关问题 如何从父指令/控制器访问指令范围? - How do I access a directive scope from a parent directive/controller? 使用“作为控制器”语法时,如何在子控制器中引用父级的作用域? - How do I refer to the scope of a parent inside a child controller when using the “as controller” syntax? 如何使用“controller as”语法继承基本控制器? - How to inherit from base controller using “controller as” syntax? 如何使用Angular UI-Grid默认设置父控制器并从中继承? - How do I set up a parent controller with Angular UI-Grid defaults and inherit from it? 我如何使用Angular将变量从指令传递给控制器 - How do I pass a variable from directive to a controller using Angular 在JavaScript中,如何使用单个“ .prototype”块从子类的父类继承? - In JavaScript, how do I inherit from a parent class in child class using a single “.prototype” block? Angular Directive - 我可以从父级继承隔离范围吗? - Angular Directive - can I have an isolate scope inherit from parent? 使用controllerAs语法通过指令$ watch更改父控制器模型 - Change parent controller model through directive $watch using controllerAs syntax 如何访问从父指令传递但在控制器中定义的子指令中的功能? - How can I access the function in child directive which passed from parent directive but defined in controller? 如何使用VueJS中的计算属性从v-for指令过滤器中删除重复项? - How do I remove repetition from a v-for directive filter using computed properties in VueJS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM