Vur*_*ral 283 json dictionary angularjs angularjs-ng-repeat
我知道我们可以轻松地对json对象或数组使用ng-repeat,如:
<div ng-repeat="user in users"></div>
Run Code Online (Sandbox Code Playgroud)
但是我们如何才能将ng-repeat用于词典,例如:
var users = null;
users["182982"] = "{...json-object...}";
users["198784"] = "{...json-object...}";
users["119827"] = "{...json-object...}";
Run Code Online (Sandbox Code Playgroud)
我想用用户词典:
<div ng-repeat="user in users"></div>
Run Code Online (Sandbox Code Playgroud)
可能吗?.如果是,我怎么能在AngularJs中做到这一点?
我的问题示例:在C#中我们定义了如下字典:
Dictionary<key,value> dict = new Dictionary<key,value>();
//and then we can search for values, without knowing the keys
foreach(var val in dict.Values)
{
}
Run Code Online (Sandbox Code Playgroud)
是否有内置函数从c#中返回字典中的值?
Art*_*eev 554
您可以使用
<li ng-repeat="(name, age) in items">{{name}}: {{age}}</li>
Run Code Online (Sandbox Code Playgroud)
请参阅ngRepeat文档.示例:http://jsfiddle.net/WRtqV/1/
Tom*_*Tom 27
我还想提一下AngularJSng-repeat的新功能,即特殊的重复起点和终点.添加了该功能是为了重复一系列 HTML元素,而不是仅重复单个父HTML元素.
要使用转发器的起点和终点,必须分别使用ng-repeat-start和ng-repeat-end指令来定义它们.
该ng-repeat-start指令与ng-repeat指令非常相似.区别在于将重复所有 HTML元素(包括它定义的标记),直到ng-repeat-end放置的结束HTML标记(包括标记ng-repeat-end).
示例代码(来自控制器):
// ...
$scope.users = {};
$scope.users["182982"] = {name:"John", age: 30};
$scope.users["198784"] = {name:"Antonio", age: 32};
$scope.users["119827"] = {name:"Stephan", age: 18};
// ...
Run Code Online (Sandbox Code Playgroud)
示例HTML模板:
<div ng-repeat-start="(id, user) in users">
==== User details ====
</div>
<div>
<span>{{$index+1}}. </span>
<strong>{{id}} </strong>
<span class="name">{{user.name}} </span>
<span class="age">({{user.age}})</span>
</div>
<div ng-if="!$first">
<img src="/some_image.jpg" alt="some img" title="some img" />
</div>
<div ng-repeat-end>
======================
</div>
Run Code Online (Sandbox Code Playgroud)
输出看起来类似于以下内容(取决于HTML样式):
==== User details ====
1. 119827 Stephan (18)
======================
==== User details ====
2. 182982 John (30)
[sample image goes here]
======================
==== User details ====
3. 198784 Antonio (32)
[sample image goes here]
======================
Run Code Online (Sandbox Code Playgroud)
如您所见,ng-repeat-start重复所有HTML元素(包括元素ng-repeat-start).所有ng-repeat特殊性能(在这种情况下$first和$index)也按预期工作.
JavaScript开发人员倾向于将上述数据结构称为对象或哈希而不是字典.
当您将users对象初始化为null时,上面的语法是错误的.我认为这是一个拼写错误,因为代码应该是:
// Initialize users as a new hash.
var users = {};
users["182982"] = "...";
Run Code Online (Sandbox Code Playgroud)
要从哈希中检索所有值,您需要使用for循环迭代它:
function getValues (hash) {
var values = [];
for (var key in hash) {
// Ensure that the `key` is actually a member of the hash and not
// a member of the `prototype`.
// see: http://javascript.crockford.com/code.html#for%20statement
if (hash.hasOwnProperty(key)) {
values.push(key);
}
}
return values;
};
Run Code Online (Sandbox Code Playgroud)
如果您计划在JavaScript中使用数据结构进行大量工作,那么下划线.js库绝对值得一看.Underscore附带了一个values方法,可以为您执行上述任务:
var values = _.values(users);
Run Code Online (Sandbox Code Playgroud)
我自己不使用Angular,但我很确定会有一个方便的方法来迭代哈希的值(啊,我们去,Artem Andreev提供上面的答案:))
| 归档时间: |
|
| 查看次数: |
168862 次 |
| 最近记录: |