angular.js - AngularJS在ng-repeat内使用ng-model出现的问题

【字号: 日期:2023-02-02浏览:65作者:雯心

问题描述

fiddle链接:http://jsfiddle.net/08tox9k4/

<html ng-app = ’test’><head></head><body ng-controller = ’testCtrl’> <p ng-repeat='qq in obj.qqs'> <input ng-model='obj.qqs[$index]' placeholder='请输入'>; </p> <script src='https://www.6hehe.com/wenda/angular.min.js'></script> <script type='text/javascript'>angular.module(’test’,[]) .controller(’testCtrl’, [’$scope’, function($scope){$scope.obj = { qqs: ['12345','23456']}; }]); </script></body></html>

问题: 当在生成的input标签内修改数值的时候每修改一位,就失去鼠标焦点,可能是因为数据的更改刷新了视图。如何能在input内完整的修改完数值?然后通过保存按钮什么的保存修改?或者说如何暂时性取消对变量的监视?

问题解答

回答1:

有一些问题:1. ng-model='obj.qqs[$index]' 应该是ng-model='qq' 这个就可以解决focus消失的问题。2. 一般不把primitive type的变量作为ng-model, 这里可以用literal object。

['12345','23456'] => [{val: '12345'}, {val: '23456'}]

那么ng-model也要修改成qq.val3. 如何通过按钮保存,用ng-click即可。4. Style的问题。用’controller as ctrl’的语法,避免在controller里面使用$scope。详见下面的sample

Working sample: http://jsfiddle.net/wfh04vhc/

回答2:

AngularJS ng-repeat下使用ng-model详解:链接描述

回答3:

直接 ng-model='qq' 就行了吧

回答4:

http://www.lovelucy.info/understanding-scopes-in-angularjs.html

相关文章: