试图从一组对象angularjs返回单个对象

时间:2017-03-15 20:22:06

标签: javascript angularjs arrays angularjs-ng-repeat

我发出一个get请求,返回一个对象数组。我希望循环遍历数组并根据该对象的name属性将一个对象返回到视图。我尝试将ng-repeatfilter一起使用,它只返回所有对象。

我是否可以过滤数据以从数组中返回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>

控制器

 $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();

0 个答案:

没有答案