Hi tôi đang cố gắng để hiển thị các thành phần đơn giản trong angularjs nơi con cần phải truy cập vào mẹ name.And mã của tôi đi như thế này:Làm thế nào để truy cập vào phần phụ huynh trong angularjs 1,5
HTML file:
<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>
Mã sản phẩm:
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);
}
});
Và nó ném một lỗi:
lỗi Loại: không thể đọc đúng ty 'myName' của undefined
Tôi không thể tìm ra những gì sai trong mã và lý do tại sao nó không thể tìm thấy parent.Any đầu vào sai lầm tôi đang làm. Có vẻ là một nhỏ tôi có thể đã bỏ lỡ.
hello và hello1 là phạm vi anh chị em, sau khi kiểm tra điều đó! –
Tôi nghĩ có điều gì đó sai với cấu trúc thành phần của bạn. Một thành phần không nên biết gì về thành phần cha. Đây là thực hành tốt nhất về thiết kế thành phần. – scokmen