[英]Angular Js: How do I select an expression in an array based on ID?
How do I print an expression of an array based on it's assigned id? 如何根据分配的ID打印数组的表达式? I have two arrays, one with the id of 11 and the other with 12. How do I get the name expression of the array with the property of 12?
我有两个数组,一个ID为11,另一个为12。如何获取属性为12的数组的名称表达式?
Below is the services module, controller and HTML 以下是服务模块,控制器和HTML
http://jsfiddle.net/ADukg/4705/ http://jsfiddle.net/ADukg/4705/
var myApp = angular.module('myApp',[]);
function MyCtrl($scope, myProperties) {
$scope.name = 'StackOverflow';
$scope.myProperties = myProperties;
};
myApp.factory('myProperties', function() {
var myProperties = [
{
'name': '3 Bedroom Fixer Upper',
'city': 'Santa Rosa',
'id': 11,
'state': 'CA',
'date_added': '2/3/14',
'completion_status': '80%',
'imgURL': 'http://dfon12yb6dcdf.cloudfront.net/product/kr4f0uv5tkd6slvmn7r9j4h4jj/w1024x683.jpg?v=4',
},
{
'name': '2 Bedroom Condo',
'city': 'Vallejo',
'id': 12,
'state': 'CA',
'date_added': '4/10/20',
'completion_status': '50%',
'imgURL': 'http://dfon12yb6dcdf.cloudfront.net/product/kr4f0uv5tkd6slvmn7r9j4h4jj/w1024x683.jpg?v=4',
},
]
//return the array here
return myProperties
});
<div ng-controller="MyCtrl">
<h1>Hello, {{name}}!</h1>
<br>
<ul ng-repeat="property in myProperties">
<li>{{property.name}}</li>
</ul>
<br>
<h3>{{property.id[12]}}</h3>
</div>
You can use angular's forEach function to compare an ID with each object in an array. 您可以使用angular的forEach函数将ID与数组中的每个对象进行比较。 Here's the relevant bit.
这是相关的位。
$scope.findObj = function(searchId)
{
var resultObj;
angular.forEach(myProperties, function(obj)
{
if ( obj.id == searchId)
{
resultObj = obj;
}
});
return resultObj;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.