我试图在单击单选按钮时获取值。然后控制器被加载,但是当执行click事件时,控制器中定义的函数没有被执行。 我在这里缺少什么?
我的代码: 的 JS
;(function() {
'use strict';
angular
.module('tramsConsole',[])
.controller('TremorController', TremorController);
TremorController.$inject = ['$scope', '$log'];
function TremorController($scope, $log,tremorService) {
var vm = this;
console.log("controller loaded");
vm.getstatus = getstatus;
function getstatus(obj){
vm.status = obj.value;
alert("clicked");
console.log(obj);
}
}
})();
HTML
<body ng-app="tramsConsole">
<div ng-controller="TremorController as tremorController">
<input type="radio" name="data" ng-value="true" ng-model="processState.widgetInstance.configuration.data.NUM_ERRORS" ng-click="getstatus($event)">NUM_ERRORS<br>
<input type="radio" name="data" ng-value="true" ng-model="processState.widgetInstance.configuration.data.NUM_UB_OCCURRENCES" ng-click="getstatus($event)">NUM_UB_OCCURRENCES<br>
<input type="radio" name="data" ng-value="true" ng-model="processState.widgetInstance.configuration.data.NUM_T_OCCURRENCES" ng-click="getstatus($event)">NUM_T_OCCURRENCES<br>
<input type="radio" name="data" ng-value="true" ng-model="processState.widgetInstance.configuration.data.NUM_OCCURRENCES" ng-click="getstatus($event)">NUM_OCCURRENCES<br>
<input type="radio" name="data" ng-value="true" ng-model="processState.widgetInstance.configuration.data.AVG_RSP_TIME" ng-click="getstatus($event)">AVG_RSP_TIME<br>
<input type="radio" name="data" ng-value="true" ng-model="processState.widgetInstance.configuration.data.UB_AVG_RSP_TIME" ng-click="getstatus($event)">UB_AVG_RSP_TIME<br>
<input type="radio" name="data" ng-value="true" ng-model="processState.widgetInstance.configuration.data.T_AVG_RSP_TIME" ng-click="getstatus($event)">T_AVG_RSP_TIME<br>
<input type="radio" name="data" ng-value="true" ng-model="processState.widgetInstance.configuration.data.UB_SQR_AVG_RSP_TIME" ng-click="getstatus($event)">UB_SQR_AVG_RSP_TIME<br>
<input type="radio" name="data" ng-value="true" ng-model="processState.widgetInstance.configuration.data.T_SQR_AVG_RSP_TIME" ng-click="getstatus($event)">T_SQR_AVG_RSP_TIME<br>
<input type="radio" name="data" ng-value="true" ng-model="processState.widgetInstance.configuration.data.SQR_AVG_RSP_TIME" ng-click="getstatus($event)">SQR_AVG_RSP_TIME<br>
</div>
</body>
plunker中的工作副本:Working Copy
答案 0 :(得分:1)
在html中调用你的方法
tremorController.getstatus($事件)
当使用 controllerAs 语法时,您必须能够按对象访问方法和变量,否则它将引用父范围对象。
答案 1 :(得分:1)
如果您使用controller as
,则$scope
会附加到控制器对象本身。
所以基本上你应该将ng-controller
标签更改为:
<div ng-controller="TremorController as vm">
并将ng-click
更改为
ng-click="vm.getstatus($event)"
它会起作用。
Plunker示例:http://plnkr.co/edit/zYqk5pciy8oxIMv28CXx?p=preview
了解详情答案 2 :(得分:0)
您需要使用控制器的html引用。
<div ng-controller="TremorController as tremorController">
<input type="radio" name="data" ng-value="true" ng-model="processState.widgetInstance.configuration.data.NUM_ERRORS" ng-click="tremorController.getstatus($event)">NUM_ERRORS<br>
<input type="radio" name="data" ng-value="true" ng-model="processState.widgetInstance.configuration.data.NUM_UB_OCCURRENCES" ng-click="tremorController.getstatus($event)">NUM_UB_OCCURRENCES<br>
<input type="radio" name="data" ng-value="true" ng-model="processState.widgetInstance.configuration.data.NUM_T_OCCURRENCES" ng-click="tremorController.getstatus($event)">NUM_T_OCCURRENCES<br>
<input type="radio" name="data" ng-value="true" ng-model="processState.widgetInstance.configuration.data.NUM_OCCURRENCES" ng-click="tremorController.getstatus($event)">NUM_OCCURRENCES<br>
<input type="radio" name="data" ng-value="true" ng-model="processState.widgetInstance.configuration.data.AVG_RSP_TIME" ng-click="tremorController.getstatus($event)">AVG_RSP_TIME<br>
<input type="radio" name="data" ng-value="true" ng-model="processState.widgetInstance.configuration.data.UB_AVG_RSP_TIME" ng-click="tremorController.getstatus($event)">UB_AVG_RSP_TIME<br>
<input type="radio" name="data" ng-value="true" ng-model="processState.widgetInstance.configuration.data.T_AVG_RSP_TIME" ng-click="tremorController.getstatus($event)">T_AVG_RSP_TIME<br>
<input type="radio" name="data" ng-value="true" ng-model="processState.widgetInstance.configuration.data.UB_SQR_AVG_RSP_TIME" ng-click="tremorController.getstatus($event)">UB_SQR_AVG_RSP_TIME<br>
<input type="radio" name="data" ng-value="true" ng-model="processState.widgetInstance.configuration.data.T_SQR_AVG_RSP_TIME" ng-click="tremorController.getstatus($event)">T_SQR_AVG_RSP_TIME<br>
<input type="radio" name="data" ng-value="true" ng-model="processState.widgetInstance.configuration.data.SQR_AVG_RSP_TIME" ng-click="tremorController.getstatus($event)">SQR_AVG_RSP_TIME<br>
</div>
他们的关键是 tremorController .getstatus($ event)