【问题标题】:Why ngModel is not updating the value for the first time?为什么 ngModel 没有第一次更新值?
【发布时间】:2014-04-23 16:48:40
【问题描述】:

为什么 ng-Model 值没有显示更新后的值

请检查我下面的代码

JS

$scope.test='xx';
$scope.testfunc=function (a) {   
  alert(a);
 }

HTML

<input type="radio" ng-model="test" ng-click="testfunc(test)" value="aaa"/>

当我点击收音机时,我应该收到警报消息“aaa”,但我收到消息“xx”。

谁能告诉我哪里出错了

【问题讨论】:

    标签: angularjs angularjs-scope angular-ngmodel


    【解决方案1】:

    radio的值由模型改变。

    最初收音机的值为“aaa”,然后在角度“boots”之后,如果您使用浏览器开发工具(chrome、firefox 或 IE10 及更高版本)检查元素,则 rasio 的值将更改为 xx ,你应该会看到的。

    您的收音机在角完成链接(启动)后是这样的

    <input type="radio" ng-model="test" ng-click="testfunc(test)" value="xx"/>
    

    【讨论】:

    • 解决办法是什么
    • 设置 $scope.test='aaa' 或从您的输入字段中删除 ng-model 指令。你的用例是什么?
    • 使用“ng-change”而不是 ng-click
    【解决方案2】:

    你的问题是你绑定了 scope.text='xx';这与 value='aaa' 无关

    ng-model=test 正在引用 $scope.test。

    如果您希望它是“aaa”,则需要将其绑定到 scope.test,然后将其作为参数传递给 html 中的函数。

    解决方案是将其作为参数传递:

    <input type="radio" ng-model="test" ng-click="testfunc('xx')" value="xx"/>
    

    或使用 jQuery:

    $scope.test = $('input:radio').val();
    

    原版JS

    $scope.test = document.querySelectorAll('input:radio');
    

    【讨论】:

    • 如果有的话可以分享一下例子吗
    猜你喜欢
    • 2020-10-31
    • 1970-01-01
    • 2023-01-15
    • 2016-10-30
    • 1970-01-01
    • 2019-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多