如何在Angularjs中使用$ event更新事件点击的ng-model [英] How to update ng-model on event click using $event in Angularjs

查看:307
本文介绍了如何在Angularjs中使用$ event更新事件点击的ng-model的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://plnkr.co/ywhmyO

我已经尝试过编译和指令,但我没有到任何地方。示例中的编译代码直接从有角度的网站上删除,但我不知道如何使用它。任何指导都非常感激。

I have tried compile and directives but I'm not getting anywhere. the compile code in the example is ripped straight off the angular website but i've no idea how to use it. Any guidance much appreciated.

推荐答案

你正在绊倒 ng-repeat 创建一个新的范围。所以您的更新仅在子范围内发生,不会显示在父范围内。

You are tripping over the fact that ng-repeat creates a new scope. So your updates are happening in the child scope only and not showing up in the parent scope.

您可以在此视图中执行此操作:

You could do something like this in your view:

<div ng-repeat="name in names">
  <input ng-model="name.name" ng-click="changeName($index)" value="{{ name.name }}">
</div>

$ index 然后在你的控制器中:

$index in the 0 based counter. Then in your controller:

$scope.changeName = function($index) {
    $scope.names[$index].name = $scope.nameselected;
};

这篇关于如何在Angularjs中使用$ event更新事件点击的ng-model的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆