Javascript AngularJS使用筛选器搜索不工作
我正在尝试使用angularjs过滤器方法进行搜索,但它不起作用。我没有在控制台中得到任何错误,但仍然没有显示搜索结果。有人能帮我吗 这是我的controller.js代码:Javascript AngularJS使用筛选器搜索不工作,javascript,angularjs,Javascript,Angularjs,我正在尝试使用angularjs过滤器方法进行搜索,但它不起作用。我没有在控制台中得到任何错误,但仍然没有显示搜索结果。有人能帮我吗 这是我的controller.js代码: .controller('SearchController', [ '$scope', 'dataService', '$location', '$routePa
.controller('SearchController',
[
'$scope',
'dataService',
'$location',
'$routeParams',
function ($scope, dataService, $location, $routeParams){
$scope.searchMovies = [ ];
$scope.searchCount = 0;
var getSearchResult = function (terms) {
dataService.getSearchResult(terms).then(
function (response) {
$scope.searchCount = response.rowCount + ' movies';
$scope.searchMovies = response.data;
$scope.showSuccessMessage = true;
$scope.successMessage = "All movie Success";
},
function (err){
$scope.status = 'Unable to load data ' + err;
}
); // end of getStudents().then
};
if ($routeParams && $routeParams.term) {
console.log($routeParams.term);
getSearchResult($routeParams.term);
}
}
]
);
这是services.js代码:
this.getSearchResult = function (terms) {
var defer = $q.defer(),
data = {
action: 'search',
term: terms
}
$http.get(urlBase, {params: data, cache: true}).
success(function(response){
defer.resolve({
data: response.ResultSet.Result,
rowCount: response.RowCount
});
}).
error(function(err){
defer.reject(err);
});
return defer.promise;
};
这是我的app.js代码:
. when('/search', {
templateUrl : 'js/partials/search.html',
controller : 'SearchController'
}).
这是search.html代码:
<div>
<label>Search: <input ng-model="searchMovie"></label><br><br><br><br>
</div>
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Category</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="search in searchMovies | filter:searchMovie">
<td>
{{search.title}}
</td>
<td>
{{search.description}}
</td>
<td>
{{search.name}}
</td>
</tr>
</tbody>
</table>
搜索:
标题
描述
类别
{{search.title}
{{search.description}
{{search.name}
正在从数据库检索搜索数据。我已经测试了SQL,它工作得很好。问题就出在angularjs服务器端。谢谢。您需要在app.js文件中为控制器提供别名
.when('/search', {
templateUrl : 'js/partials/search.html',
controller : 'SearchController'
controllerAs: 'movies',
});
现在在您的search.html文件中传递controllerAs
<div>
<label>Search: <input ng-model="searchMovie"></label><br><br><br><br>
</div>
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Category</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="search in searchMovies | filter:searchMovie">
<td>
{{movies.search.title}}
</td>
<td>
{{movies.search.description}}
</td>
<td>
{{movies.search.name}}
</td>
</tr>
</tbody>
搜索:
标题
描述
类别
{{movies.search.title}
{{movies.search.description}
{{movies.search.name}
您需要在app.js文件中为控制器指定别名
.when('/search', {
templateUrl : 'js/partials/search.html',
controller : 'SearchController'
controllerAs: 'movies',
});
现在在您的search.html文件中传递controllerAs
<div>
<label>Search: <input ng-model="searchMovie"></label><br><br><br><br>
</div>
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Category</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="search in searchMovies | filter:searchMovie">
<td>
{{movies.search.title}}
</td>
<td>
{{movies.search.description}}
</td>
<td>
{{movies.search.name}}
</td>
</tr>
</tbody>
搜索:
标题
描述
类别
{{movies.search.title}
{{movies.search.description}
{{movies.search.name}
@StarsSky我在问题中添加了html代码您好,您如何设置url参数?如果没有设置,您的searchMovies数组将保持为空。您指的是@NoraDoes这部分执行的url参数:
If($routeParams&&$routeParams.term){console.log($routeParams.term);getSearchResult($routeParams.term);}
@StarsSky我在问题中添加了html代码您好,如何设置url参数?如果未设置,您的searchMovies数组将保持为空。您指的是@NoraDoes此部分执行的url参数:If($routeParams&&$routeParams.term){console.log($routeParams.term);getSearchResult($routeParams.term);}