Javascript 尝试从对象数组返回单个对象angularjs
我发出一个get请求,返回一个对象数组。我希望循环遍历数组,并根据该对象的name属性将单个对象返回到视图。我曾尝试将Javascript 尝试从对象数组返回单个对象angularjs,javascript,angularjs,arrays,angularjs-ng-repeat,Javascript,Angularjs,Arrays,Angularjs Ng Repeat,我发出一个get请求,返回一个对象数组。我希望循环遍历数组,并根据该对象的name属性将单个对象返回到视图。我曾尝试将ng repeat与过滤器一起使用,它只返回所有对象 我可以过滤数据,从数组中返回1个对象,而不用使用搜索框。我希望在加载页面时发生这种情况 HTML <div ng-repeat="lecturer in lecturers | filter:name: courses.lecturer"> <h3>{{lecturer.name}}&
ng repeat
与过滤器一起使用,它只返回所有对象
我可以过滤数据,从数组中返回1个对象,而不用使用搜索框。我希望在加载页面时发生这种情况
HTML
<div ng-repeat="lecturer in lecturers | filter:name: courses.lecturer">
<h3>{{lecturer.name}}</h3>
<div>
<img class="img-circle" ng-src="{{lecturer.picture}}" alt="" />
</div>
<p>{{lecturer.description}}</p>
</div>
如果我理解正确,您的函数讲师fac.get讲师代码([name])
返回一个讲师数组,您只需要一个?您只需执行以下操作::$scope.讲师=reponse.data[0]
。这将为您提供工厂职能部门返回的第一位讲师。它们已经从服务器上按名称进行了筛选(我假设,基于函数的名称),因此您无论如何都无法在客户端对它们进行进一步筛选,除非您使用不同的筛选器(看起来不像)。您的筛选器不正常。请尝试|筛选器:{name:courses.讲师}
。请参阅。@Claies筛选器也不相关。他只想介绍一位讲师,所以一开始就不需要使用ng repeat
。@Claies,这很有效。总是小事
$scope.lecturers;
$scope.courses;
$scope.status;
var id = $stateParams.id;
//getCourse() method calls the coursesFac factory/service which calls the api endpoint and returns
//the course information based on the course id.
$scope.getCourse = function(id){
coursesFac.getCourseById(id)
.then(function (response) {
// var items;
response.data.video = JSON.parse(response.data.video);
console.log(response.data.video);
items = response.data.video;
$scope.courses = response.data;
// console.log(items);
// console.log($scope.courses.video);
}, function (error) {
$scope.status = 'Unable to load course data: ' + error.message;
console.log($scope.status);
});
};
//passes the state paramater in to the method. The paramater is the course Id
$scope.getCourse(id);
$scope.getLecturer = function(){
lecturerFac.getLecturer()
.then(function (response) {
$scope.lecturers = response.data;
console.log("inside the function getLectuers" +$scope.lecturers);
}, function (error) {
$scope.status = 'Unable to load lecturer data: ' + error.message;
console.log($scope.status);
});
};
$scope.getLecturer();