简体   繁体   English

角度-Ng变化返回值始终相同

[英]Angular - Ng-change return always the same value

I would like to ask, how can i get selected value from radio list? 我想问一下,如何从广播列表中获取选定的值?

I tried ng-change, and watch scope, but i get always only first value which is set into the model during the app initi. 我尝试了ng-change,并观察了作用域,但始终只有在应用程序初始化期间设置到模型中的第一个值。

Here is example of my code: 这是我的代码示例:

Radio button group: 单选按钮组:

<div class="list" ng-contoller="SettingsCtrl">
        <ion-radio ng-model="choice" ng-value="'dials'">Dials</ion-radio>
        <ion-radio ng-model="choice" ng-value="'conversations'">Conversations</ion-radio>
        <ion-radio ng-model="choice" ng-value="'appoitnments'">Appointments</ion-radio>
        <ion-radio ng-model="choice" ng-value="'orders'">Orders</ion-radio>
      </div>

JS: JS:

// Default choice scope
$scope.choice;

$scope.$watch('choice', function(value) {
            // Here i get always the same value
            console.log("Selected goalType, text: " +value);
});

How can i get selected value and set this value into the scope? 如何获得选定的值并将其设置到示波器中?

Thanks for any help. 谢谢你的帮助。

Without using ionic framework and with input radio buttons, it is working. 在不使用离子框架和输入单选按钮的情况下,它可以正常工作。 You've just make a mistake on this line <div class="list" ng-contoller="SettingsCtrl"> . 您在这行<div class="list" ng-contoller="SettingsCtrl">上犯了一个错误。 You've forgotten an r in controller. 您忘记了控制器中的r。

Here is the code working : HTML 这是工作代码:HTML

<!DOCTYPE html>
<html ng-app="app">

  <head>
    <script data-require="angular.js@1.2.25" data-semver="1.2.25" src="https://code.angularjs.org/1.2.25/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body >
    <div class="list" ng-controller="SettingsCtrl">
        <input type="radio" ng-model="choice" ng-value="'dials'">Dials
        <input type="radio" ng-model="choice" ng-value="'conversations'">Conversations
        <input type="radio" ng-model="choice" ng-value="'appoitnments'">Appointments
        <input type="radio" ng-model="choice" ng-value="'orders'">Orders
      </div>
  </body>

</html>

And JavaScript : 和JavaScript:

var app = angular.module('app', []);

app.controller('SettingsCtrl', function($scope){
  $scope.choice;
  console.log('ctrl');
  $scope.$watch('choice', function(value) {
              // Here i get always the same value
              console.log("Selected goalType, text: " +value);
  });
});

You can see it working here : http://plnkr.co/edit/TRJKW7eOlBek6TditM1B?p=preview 您可以在这里看到它的工作: http : //plnkr.co/edit/TRJKW7eOlBek6TditM1B?p=preview

EDIT I've tested it with ionic framework. 编辑我已经用离子框架对其进行了测试。 It is also working. 它也正在工作。 So it was just the r the issue. 因此,这只是问题所在。

I've made this fiddle for you. 我为你做了这个小提琴 When you select a radio , $scope.currentSelection changes and this way you'll know which is the current selected goal. 选择radio$scope.currentSelection更改,这样您将知道哪个是当前选定的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM