[英]AngularJS: use $scope.$watch with controller as syntax
I have a plnkr here that I am testing $scope.$watch on radio buttons with the controller as syntax
.我在这里有一个 plnkr,我正在使用
controller as syntax
在单选按钮上测试$scope.$watch 。 In addition, the radio buttons are embedded inside a parent view container.此外,单选按钮嵌入在父视图容器中。
In the 'About us' page, I have the following html code for showing two radio buttons along with the current selection message:在“关于我们”页面中,我有以下 html 代码用于显示两个单选按钮以及当前选择消息:
<div class="row-fluid">
<div class="well">
<p><strong>Make a selection:</strong></p>
<label>Yes</label>
<input type="radio" name="selection" ng-model="aboutView.radioSelection" value="Yes">
<label>No</label>
<input type="radio" name="selection" ng-model="aboutView.radioSelection" value="No">
<p>{{aboutView.message}}</p>
</div>
I have the following code in the aboutController.js:我在 aboutController.js 中有以下代码:
(function() {
'use strict';
angular.module('layoutApp')
.controller('aboutController', aboutController);
aboutController.$inject = ['$scope'];
function aboutController($scope) {
var vm = this;
vm.radioSelection = "No"; //default selection
vm.message = "Your selection is " + vm.radioSelection;
/**
* **This does not have any response, not working**
$scope.$watch(vm.radioSelection, function(newVal, oldVal) {
vm.message = newVal;
}, true);
*/
}
}())
Lastly, I have the following block of code in my ui.router config that is relevant to the about us page:最后,我的 ui.router 配置中有以下与关于我们页面相关的代码块:
.state('home.about', {
url: '/about',
templateUrl: 'about.html',
controller: 'aboutController as aboutView'
})
You can try this one, use 'aboutView' as you set in controller As instead of vm.你可以试试这个,使用你在控制器 As 中设置的 'aboutView' 而不是 vm。
$scope.$watch('aboutView.radioSelection', function(newVal, oldVal) {
vm.message = "Your selection is " + newVal;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.