加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS:两个带有单独ngModel的输入,一个更新其他值

发布时间:2020-12-17 17:30:47 所属栏目:安全 来源:网络整理
导读:我正在尝试使用两个单独的输入字段,每个输入字段都有自己的模型,其中一个输出字段默认更新另一个的值. form id="registerForm" ng-submit="create(email,username,password)" input type="text" name="email" placeholder="Email" ng-model="email" input ty
我正在尝试使用两个单独的输入字段,每个输入字段都有自己的模型,其中一个输出字段默认更新另一个的值.

<form id="registerForm" ng-submit="create(email,username,password)">
    <input type="text" name="email" placeholder="Email" ng-model="email">
    <input type="text" name="username" value="{{email}}" placeholder="Username" ng-model="username">
    <input type="password" name="password" placeholder="Password" ng-model="password">

    <input type="submit" value="register" class="button" ng-show="!loading">
    <input type="submit" value="processing" class="button" disabled ng-show="loading">
</form>

结果是,如果您输入电子邮件,用户输入将自动填充,但如果特别输入用户名,则可以更改用户名.

但这不起作用.相反,ng-model =“username”会覆盖值attr,无论在那里设置什么.因此,用户名输入保持为空,直到明确输入为止.

有没有办法在这些方面做到这一点,还是需要特殊的指令或什么?

解决方法

不要使用价值:

<input type="text" name="username" placeholder="Username" ng-model="username">

在控制器中使用$watch:

function MyCtrl($scope) {
    $scope.$watch('email',function(value) {
        $scope.username = value
    });
}

fiddle

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读