问题
my controller bring data from spring app data but it does not get displayed in form. When I put constants in data it works perfectly fine.
here is my code in JS controller
sampleApp.controller('searchCourseController', ['$scope', '$http',
function($scope, $http, $routeParams, ngTableParams ) {
$scope.courses = {};
$scope.searchButton=function () {
var actionUrl = 'searchCourse';
var textToSearch = $("#txtTitle").val();
if (textToSearch.length == 0) {
alert("Please provide search criteria. Blank search is not allowed");
/* $("#loader-overlay").fadeOut();
$("#bg-loader").fadeOut(); */
return;
}
$http.get(actionUrl+"?title="+escape(textToSearch))
.success(
function(data, status, headers, config) {
$scope.courses = data;
console.log($scope.courses);
})
.error(
function(data, status, headers, config) {
});
};
$scope.editCourseButton=function () {
alert ("edit");
};
}]);
and my html that i am ussing to display data is following
<table class="table">
<thead>
<tr>
<th>Course Name</th>
<th>Status</th>
<th class="hidden-xs">Publishing status</th>
<th class="hidden-xs">Group Name</th>
<th class="hidden-xs">Rating</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="course in $scope.courses">
<td>{{course.name}}</td>
<td>{{course.courseStatus}}</td>
<td>{{course.coursePublishStatus}}</td>
<td>{{course.courseGroupName}}</td>
<td>{{course.courseRating}}</td>
</tr>
</tbody>
</table>
What could be the problem as the same code with fixed data gets displayed in data table without an issue.
thanks,
回答1:
You can't specify $scope in the view, since it's not defined:
<tr ng-repeat="course in $scope.courses">
Becomes
<tr ng-repeat="course in courses">
回答2:
As well as omitting the $scope from $scope.courses inside the ng-repeat, you may want to use a $scope.$apply() inside your $http.get success callback to update the scope. This will update the view to the updated $scope.courses binding after a asynchronous process.
Example:
$http.get(actionUrl+"?title="+escape(textToSearch))
.success(function(data, status, headers, config) {
$scope.courses = data;
$scope.$apply(); // used $scope.$apply() to update the scope.
console.log($scope.courses);
})
.error(
function(data, status, headers, config) {
});
});
回答3:
Calling search function on ng-change like following
<input class="form-control" type="text" placeholder="Title" id="txtTitle" ng-change="search()" ng-model="txtToSearch">
solved the issue. but I am still trying to find out why it isn't working on search button. change search controller as well and here is code
function SearchCtrl($scope, $http, $location, CS) {
$scope.search = function() {
$scope.result = null;
$http({method: 'GET', url: '/search?title=' + $scope.txtToSearch }).
success(function(data, status, headers, config) {
$scope.results = data;
}).
error(function(data, status, headers, config) {
$scope.status = status;
});
};
$scope.edit=function(c)
{
CourseService.set(course);
$location.path('/edit');
}
}
来源:https://stackoverflow.com/questions/24087167/angularjs-ng-repeat-is-not-showing-table-data