ng checked和ng change单选按钮不一起工作-angularjs

ng checked和ng change单选按钮不一起工作-angularjs,angularjs,angularjs-ng-model,angularjs-ng-change,angularjs-ng-checked,Angularjs,Angularjs Ng Model,Angularjs Ng Change,Angularjs Ng Checked,js 和html {{radius.name} 注意,“2英里”无线电输入根据半径范围结构进行检查。为什么ng更改不会触发该功能 如果我添加ng模型,该函数将触发,但选中的ng不起作用。这是因为您没有使用ng模型。: <div class="radio" ng-repeat="radius in radii" id="selectradius-{{radius.id}}"> <label> <input type="radio" name="r

js

和html


{{radius.name}

注意,“2英里”无线电输入根据半径范围结构进行检查。为什么ng更改不会触发该功能


如果我添加ng模型,该函数将触发,但选中的ng不起作用。

这是因为您没有使用
ng模型。

<div class="radio" ng-repeat="radius in radii" id="selectradius-{{radius.id}}">
  <label>
    <input type="radio" name="radius"

         ng-change="handleRadioClick(selectedRadius)"
         ng-model="radius.checked">

    {{radius.name}}

  </label>
</div>
  $scope.radii = [
    {id:.25, name:"1/4 Mile"},
    {id:.5, name:"1/2 Mile"},
    {id:1, name:"1 Mile"},
    {id:2, name:"2 Mile"},
    {id:3, name:"3 Mile"},
    {id:4, name:"4 Mile"},
    {id:5, name:"5 Mile"}
  ];

  // selected value is {id:2, name:"2 Mile"}
  $scope.selectedRadius = $scope.radii[3];
HTML

<div class="radio" ng-repeat="radius in radii" id="selectradius-{{radius.id}}">
  <label>
    <input type="radio" name="radius"

         ng-change="handleRadioClick(radius)"
         ng-model="selectedRadius"
         ng-value="radius">

    {{radius.name}}

  </label>
</div>

这是因为我们正在改变


希望这有帮助。干杯

使用默认值,一切都会像魔咒一样工作,不需要$watch scope或任何东西。。。是的,您需要ng型号

  • ng模型-所有功能都正常吗
  • ng checked-确保已选中属性present
  • ng更改-更改时将触发您的功能
HTML示例:

<input type="checkbox" 
    ng-model="row.active"
    ng-checked="row.active"
    ng-true-value="1"
    ng-false-value="0"
    ng-change="update(row)"
/>
<!-- test -->
{{ row.active }}
$scope.update = function(row) {
    console.log('row.active');
}

ng change
指令要求存在
ng model
指令

从文档中:

ngChange 注意,本指令要求提供
ngModel


ng model
ng checked
指令不应一起使用 从文档中:

NGCHECK 如果
ngChecked
中的表达式为truthy,则在元素上设置checked属性

请注意,此指令不应与
ngModel
一起使用,因为这可能导致意外行为

而是从控制器设置所需的初始值

有关详细信息,请参阅


虽然这会调用my函数,但默认收音机不再被选中。所以我放弃了,因为很明显,这两件事我都不想做。我只是希望在座的每个人都理解我的简单要求,并理解没有解决方案。加上一个复数半径:-)似乎是合乎逻辑的,但在for
ng checked
中,它说:“请注意,此指令不应与ngModel一起使用,因为这可能会导致意外行为。”
ngModel
ngChecked
不能一起使用。看见
<div class="radio" ng-repeat="radius in radii" id="selectradius-{{radius.id}}">
  <label>
    <input type="radio" name="radius"

         ng-change="handleRadioClick($parent.selectedRadius)"
         ng-model="$parent.selectedRadius"
         ng-value="radius">

    {{radius.name}}

  </label>
</div>
<input type="checkbox" 
    ng-model="row.active"
    ng-checked="row.active"
    ng-true-value="1"
    ng-false-value="0"
    ng-change="update(row)"
/>
<!-- test -->
{{ row.active }}
$scope.update = function(row) {
    console.log('row.active');
}