Angular 在不使用 $Scope 的情况下从 Controller 更新 Doom
Angular Update Doom from Controller without using $Scope
我真的需要一些帮助 angular...这次我完全不知道。
我想更新我的厄运,Angular 使用我的控制器的元素。
例如:
HTML代码:
<div ng-controller="UpdateController as UC">
{{UC.updateText}}
<button ng-click="UC.updateIt()"/>
</div>
js代码:
angular.controller('UpdateController', function(){
this.updateText = 'Text to Update';
this.updateIt = function(){
// Here i need to update this.updateText.
}
});
我试过:
angular.controller('UpdateController', function(){
var updateText = this;
updateText.newText = 'Text to Update'
this.updateIt = function(){
updateText.newText = "my new text";
}
});
我也将 HTML 更改为 {{UC.updateText.newText}}
但是它不起作用,我可以在 console.log() 中看到我所有的更改;
我尝试了多种方法。使用 Watch(错误,摘要已经 运行)。以及其他变量和调用。
有人可以向我解释如何使用 'this.' 吗?
我不想不使用 $scope,如果我使用 $scope TAG,它更新没有问题,但我不想放弃使用 'this.' 标签。
另外,我不想使用 element.angular 或 jQuery。
谢谢!
您应该可以只使用 this.updateText = 'my new text'
。但是,建议使用分配给 this
的变量,因为在控制器内部的函数中这可能会发生变化。
angular.controller('UpdateController', function(){
var vm = this;
vm.updateText = 'Text to Update';
vm.updateIt = function(){
vm.updateText = 'my new text';
}
});
我真的需要一些帮助 angular...这次我完全不知道。
我想更新我的厄运,Angular 使用我的控制器的元素。
例如:
HTML代码:
<div ng-controller="UpdateController as UC">
{{UC.updateText}}
<button ng-click="UC.updateIt()"/>
</div>
js代码:
angular.controller('UpdateController', function(){
this.updateText = 'Text to Update';
this.updateIt = function(){
// Here i need to update this.updateText.
}
});
我试过:
angular.controller('UpdateController', function(){
var updateText = this;
updateText.newText = 'Text to Update'
this.updateIt = function(){
updateText.newText = "my new text";
}
});
我也将 HTML 更改为 {{UC.updateText.newText}}
但是它不起作用,我可以在 console.log() 中看到我所有的更改; 我尝试了多种方法。使用 Watch(错误,摘要已经 运行)。以及其他变量和调用。
有人可以向我解释如何使用 'this.' 吗?
我不想不使用 $scope,如果我使用 $scope TAG,它更新没有问题,但我不想放弃使用 'this.' 标签。 另外,我不想使用 element.angular 或 jQuery。
谢谢!
您应该可以只使用 this.updateText = 'my new text'
。但是,建议使用分配给 this
的变量,因为在控制器内部的函数中这可能会发生变化。
angular.controller('UpdateController', function(){
var vm = this;
vm.updateText = 'Text to Update';
vm.updateIt = function(){
vm.updateText = 'my new text';
}
});