【发布时间】:2018-03-23 04:47:30
【问题描述】:
所以我有一个父组件,其中包含大量较小的组件。一般的想法是我需要一个组件来接收输入中的数据并将其显示在另一个组件上。这就是我所拥有的,它不太管用,之后有更多关于问题的详细信息:
const app = angular.module('app', []);
app.service('dataService', function() {
let data = {
key1: "",
key2: "",
key3: {
key4: 0,
key5: 0
}
}
this.getKey1 = function() {
return data.key1;
}
this.updateKey1 = function(str) {
data.key1 = str;
}
}
app.component('display', {
controller: displayController,
template: '<p>{{ keyOne }}</p>'
});
app.component('input', {
controller: inputController,
template: '<input ng-model="$ctrl.key1" ng-change="sendKey1()">'
}
function displayController($scope, dataService) {
const vm = this;
const self = $scope;
vm.$onInit = onInit;
function onInit() {
self.keyOne = dataService.getKey1();
}
}
function inputController($scope, dataService) {
const vm = this;
const self = $scope;
vm.$onInit = onInit;
function onInit() {
self.sendKey1 = function() {
dataService.updateKey1(vm.key1)
}
}
所以更新有效,但它没有将它传递给显示组件。如果我在更新数据对象后记录它是正确的,但它不会显示在视图中。
【问题讨论】:
标签: javascript angularjs data-binding service components