【发布时间】:2019-01-04 00:17:29
【问题描述】:
如何绑定到来自服务的 html 值(来自服务的值是动态变化的,但我的组件没有观察到它)。我只想在我的组件 html 中像文本一样显示这个变量。
类似这样的:
service.js
app.service('testService', function() {
this.testData = {
test1: 12,
test2: 13
}
this.newFunction() {
this.testData = {
test1: 22,
test2: 23
}
});
component.js
(function() {
angular.module('test.module')
.component('testComponent', {
templateUrl: '/test.html',
controllerAs: 'vm',
controller: function(testService) {
this.data = testService.testData;
this.nextPage = () => {
this.newFunction();
}
}
});
})();
html
<button ng-click="vm.newFunction()">Click</button>
所以,如果我点击按钮,我想要 data = {test1: 22, test2: 23},但我仍然有 data = {test1: 12, test2: 13}
我该如何处理?
【问题讨论】:
标签: angularjs components