AngularJS:我该如何根据ID从数组中选择一行
AngularJS: How would I go about selecting a single row from an array based on ID
这已经更新,并且是功能性的谢谢@j.wittwerhttp://jsfiddle.net/wittwerj/2xjuh/
我正试图根据ID从一系列帖子中选择一行。选择单个帖子的目的是创建一个视图,用户可以在其中对特定帖子发表评论。
我不确定最好的方法是什么。我想过只创建一个模型,但我不确定如何选择一行来实现这一点。然后我还需要用表单控制器来选择它,这样我就可以在数组中将它发送回ajax,这样它就可以在该帖子的数组中发布。
如果代码很乱,我很抱歉firefox似乎不喜欢Stack应用的格式。
这是我的JS代码:(更新(
var myApp = angular.module('myApp', []);
myApp.controller('FrmController', function ($scope, $http) {
$scope.visible = {
post: 1
};
$scope.posts = [{
id: 1,
content: 'Lorem ipsum dolor sit amet, eu laboramus persecuti cum, vel prompta ornatus democritum at, te alia partiendo pri. Ei quo sumo verear. Sed ad elitr aeterno disputationi, solum philosophia ex pro. Tempor essent prodesset in his, ne diam menandri vix, feugiat menandri ad cum.',
comment: ['first!!', 'second!!']
}, {
id: 2,
content: 'Facilisi pertinacia an nec. Veniam nostro commune ei pro, in mazim labores disputationi nec, cu habeo ludus deleniti ius. Id eripuit adolescens vis, mei nemore copiosae referrentur id. Pro ut ubique delicatissimi.',
comment: ['great post!', 'tl;dr', 'interesting']
}, {
id: 3,
content: 'Sed fugit error cu. In cetero albucius insolens pri, an sea velit altera constituto. Et perpetua splendide sed, te vel solum doming contentiones. Pro no omnes ridens liberavisse, ea pri tale cetero laoreet, pro te essent civibus assueverit. Assum essent appareat mei te, duo aeque consulatu et, te mel reque facilisis.',
comment: ['first to comment!']
} ];
$scope.btn_add = function (post, comment) {
if (comment != '') {
var IS_VALID = true;
}
if (IS_VALID) {
console.log("The form was sent");
post.comment.push(comment);
}
}
$scope.remItem = function (post, $index) {
post.comment.splice($index, 1);
}
});
HTML:(更新(
<div ng-controller="FrmController">choose a post ({{visible.post}} is visible)
<ul>
<li ng-repeat="post in posts" style="display: inline; list-style-type: none;">
<input type="button" ng-click="visible.post = post.id" value="{{post.id}}" />
</li>
</ul>
<div ng-repeat="post in posts" ng-if="visible.post == post.id">{{post.content}}
<form>Post your Comment (for post {{post.id}})
<textarea ng-model="txtcomment" placeholder="Your Comment" style='width:550px'></textarea>
<button ng-click='btn_add(post, txtcomment);txtcomment = "";' style='margin-top:10px;'>Post Comment</button>
<h4>Comments</h4>
<ul>
<li ng-repeat="comnt in post.comment">{{ comnt }}<a style="float: right;" href="" ng-click="remItem(post, $index)">x</a>
</li>
</ul>
</form>
</div>
</div>
我能想到的最简单的方法是让ng-click
设置一个变量,指示哪个帖子是可见的/被评论的。然后在ng-if
中使用该变量来显示正确的帖子。
<ul>
<li ng-repeat="post in posts">
<input type="button" ng-click="visible.post = post.id" value="{{post.id}}" />
</li>
</ul>
<div ng-repeat="post in posts" ng-if="visible.post == post.id">{{post.content}}
...
以下是一个工作演示:http://jsfiddle.net/wittwerj/2xjuh/
相关文章:
- 允许为显示的每个数据表选择一行
- 从 html 表中选择一行(突出显示)并在单击按钮时发送值
- koGrid-如何用程序选择一行
- 从数据表中选择一行以显示单选按钮值
- 在 jqxGrid 中按一个单元格的值选择一行
- 如何在文本区域中选择(一行或多行)
- Javascript一次选择一行
- 使用javascript选择一行中除RadGrid中的一列外的所有列
- 使用javascript在元素中选择一行文本
- 如何选择一行's单元格,并通过JavaScript获取它们的值
- 如何使用javascript在选中复选框时选择一行单选按钮
- 如何在Jquery数据表中选择一行
- 从生成的html表中选择一行
- 从html表中选择一行,单击按钮即可发送值
- HTML下选择一行<标签在UIWebView
- 引导表,动态地向表和用户添加行,然后突出显示并选择一行
- 根据内容在表中选择一行,然后单击所选行量角器中的链接
- 使用变量从html表中选择一行
- 通过单选按钮选择一行时,将一行数据复制到同一页面的文本字段中
- 如何在鼠标单击事件时从动态表中选择一行