最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - angularjs Prepopulate input text inside ng-repeat directive - Stack Overflow

programmeradmin0浏览0评论

We have input text to be shown with pre filled values. We are using list using ng-repeat directive

 <ul ng-repeat="post in postList>
   <input type="text" ng-model="postid" nginit="postid='{{post.id}}'"></input>
 </ul>

This question AngularJS - Value attribute on an input text box is ignored when there is a ng-model used? tells to how to prepopulate input text. But somehow we are finding it difficult using inside dynamic list. Can we do like this or there is any better approach?

When we this code we dont get any value in the text box but on checking in elements tab of developer console of chrome; we can see value is getting updated but not in the text box.

We have input text to be shown with pre filled values. We are using list using ng-repeat directive

 <ul ng-repeat="post in postList>
   <input type="text" ng-model="postid" nginit="postid='{{post.id}}'"></input>
 </ul>

This question AngularJS - Value attribute on an input text box is ignored when there is a ng-model used? tells to how to prepopulate input text. But somehow we are finding it difficult using inside dynamic list. Can we do like this or there is any better approach?

When we this code we dont get any value in the text box but on checking in elements tab of developer console of chrome; we can see value is getting updated but not in the text box.

Share Improve this question edited May 23, 2017 at 12:13 CommunityBot 11 silver badge asked Aug 5, 2014 at 20:03 RaichuRaichu 1071 silver badge11 bronze badges 4
  • data belongs to the model, why pre-populating a control when you should be pre-populating your model? – Dalorzo Commented Aug 5, 2014 at 20:07
  • Unrelated to your question, but the only valid child element of a ul is an li. – Steve Sanders Commented Aug 5, 2014 at 20:14
  • @Dalorzo Can you please explain a bit what we are doing wrong & how should we correct it? – Raichu Commented Aug 5, 2014 at 20:25
  • data belongs to the model like the accepted answer suggested you should bind, pre-populate and do all you data management over postList and not in your HTML View or over the controls in the HTML – Dalorzo Commented Aug 5, 2014 at 20:28
Add a ment  | 

1 Answer 1

Reset to default 7

Why don't you just bind directly to post.id? If it has a value, it will bind it to the value property of the text box.

Also, make sure you are using correct markup (input tag) and using the correct directives (ng-init). And I'm pretty sure to do not want to repeat the ul tag, but the items inside of it.

<ul>
    <li ng-repeat="post in postList">
        <input type="text" ng-model="post.id"></input>
    </li>
</ul>
发布评论

评论列表(0)

  1. 暂无评论