emk*_*a26 4 data-binding angularjs
我有父组件:
(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”。我该怎么做?我读过一些有关绑定的内容,但我不知道如何在这个示例中使用它。
感谢您的任何建议。
使用单向<
绑定:
<child-component in-data="$ctrl.parentData"></child-component>
Run Code Online (Sandbox Code Playgroud)
子组件:
app.component("childComponent", {
bindings: {
inData: '<',
},
template: `
<div>{{$ctrl.inData}}</div>
`,
})
Run Code Online (Sandbox Code Playgroud)
演示版
<child-component in-data="$ctrl.parentData"></child-component>
Run Code Online (Sandbox Code Playgroud)
app.component("childComponent", {
bindings: {
inData: '<',
},
template: `
<div>{{$ctrl.inData}}</div>
`,
})
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅
归档时间: |
|
查看次数: |
8196 次 |
最近记录: |