我有父组件:
(function () {
angular.module('app.module')
.component('parentComponent', {
templateUrl: 'parent.html',
controllerAs: 'vm',
controller: function ($scope) {
this.$onInit = () => {
$scope.parentData = 'test'
}
})
})()
Run Code Online (Sandbox Code Playgroud)
子组件
(function () {
angular.module('app.module').component('childComponent', {
templateUrl: 'child.html',
controllerAs: 'vm',
controller: function () {
this.$onInit = () => {
}
}
})
})()
Run Code Online (Sandbox Code Playgroud)
父级.html
<child-component></child-component>
Run Code Online (Sandbox Code Playgroud)
孩子.html
<p>{{parentData}}</p>
Run Code Online (Sandbox Code Playgroud)
所以我想访问子组件中的parentData,以便在子组件中显示字符串“test”。我该怎么做?我读过一些有关绑定的内容,但我不知道如何在这个示例中使用它。
感谢您的任何建议。
我将通过选中的复选框(已迭代)传递给ngModel时遇到问题。
<label class="btn btn-outline-secondary"
*ngFor="let test of tests" >
<input type="checkbox">
</label>
Run Code Online (Sandbox Code Playgroud)
在ts中我有模型:
testData = <any>{};
this.tests = [{
id: 1, name: 'test1'
},
{
id: 2, name: 'test2'
},
{
id: 3, name: 'test3'
},
]
Run Code Online (Sandbox Code Playgroud)
我尝试使用ngModel和ngModelChange,但是在显示选中复选框时仍然有问题。我怎样才能做到这一点?