こんにちは、子が親の名前にアクセスする必要がある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
コードの何が問題なのか、なぜ親が見つからないのかを理解できません。私が犯している間違いに関する入力はありません。見逃した可能性のある小さな問題のようです。