ng-click in radiobutton没有被调用

时间:2016-06-02 12:27:32

标签: angularjs

我试图在单击单选按钮时获取值。然后控制器被加载,但是当执行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

3 个答案:

答案 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

https://docs.angularjs.org/api/ng/directive/ngController

了解详情

答案 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)

updated plunker