12

您好我正在尝试在 angularjs 中显示简单的组件,其中孩子需要访问父名称。我的代码如下所示:

HTML 文件:

<html>
<head>
    <script type='text/javascript' src='angular.min-1.5.0.js'></script>
    <script type='text/javascript' src='app.js'></script>
</head>
<body ng-app="componentApp">
    <div ng-controller="helloCnt"> 
        <hello name="Parent"></hello>
        <hello1 name="Child"></hello1>  
        <label>List: <input name="namesInput" ng-model="names" ng-list=" | "   required></label>
    </div>
</body>
</html>

代码:

app.component('hello', {
        transclude: true,
        template:'<p>Hello I am {{$ctrl.name}} and ctrl name is {{myName}}</p>',
        bindings: { name: '@' },
        controller: function($scope) {
                        $scope.myName = 'Alain';
                        alert(1);
        }
});

app.component('hello1', {
        require: {
            parent: 'hello'
        },
        template:'<p>Hello I am {{$ctrl.name}} && my parent is {{myNameFromParent}} </p>',
        bindings: { name: '@' },
        controller: function($scope) {
                        $scope.myNameFromParent=this.parent.myName;
                        alert(2);
        }
});

它抛出一个错误:

TypeError: Cannot read property 'myName' of undefined

我无法弄清楚代码中有什么问题以及为什么找不到父代码。关于我所犯错误的任何输入。似乎是我可能错过的一个小错误。

4

2 回答 2

6

实际上我在用@gyc指出的答案进行了以下修改后得到了答案:

JS代码:

angular
    .module('componentApp', [])
    .controller('helloCtrl', function ($scope) {
        $scope.names = ['morpheus', 'neo', 'trinity'];
    })
    .component('hello', {
        transclude: true,
        template: '<p>Hello I am {{parentCtrl.name}} and my name is {{parentCtrl.myName}}</p><ng-transclude></ng-transclude>',
        controllerAs: 'parentCtrl',
        controller: function ($scope) {
            this.myName = 'Braid';
        },
        bindings: {
            name: '@'
        }
    })
    .component('hello1', {
        template: '<p>Hello I am {{$ctrl.name}} && my parent is {{myNameFromParent}}  </p>',
        controller: function ($scope) {
            this.$onInit = function () {
                $scope.myNameFromParent = this.parent.myName;
            };

        },
        bindings: {
            name: '@'
        },
        require: {
            parent: '^hello'
        }
    });

HTML:

<html>
<body ng-app="componentApp">
    <div ng-controller="helloCtrl">
        <hello name="Parent">
            <hello1 name="Child"></hello1>
        </hello>
        <label>List:
            <input name="namesInput" ng-model="names" ng-list=" | " required>
        </label>
    </div>
</body>
</html>

我这样做的常见错误是没有遵循嵌套组件格式并且没有在我的父级中使用 transclude。当我进行这两项更改并修改我的后续代码时,其余的工作正常。

PS - HTML中包含的ng-list与组件无关。那是为了其他目的。

@gyc - 感谢您的帮助。您的输入有所帮助。

@daan.desmedt - 我希望在组件而不是指令中找到解决方案。

于 2016-08-10T11:07:41.740 回答
5

要使用 require 继承,组件需要嵌套:

<hello name="Parent"></hello>
<hello1 name="Child"></hello1>

而是做

<hello name="Parent">
    <hello1 name="Child"></hello1>
</hello>

然后你可以像这样要求父母:

require: {
    parent: '^^hello'
  },
于 2016-08-09T12:59:23.870 回答