我有一个ng-click调用$ scope.get_post_Range,它返回一系列数据库查询。问题是,完成后,我的原始控制器主函数再次运行('post_View_Ctrl')。这将返回整个数据库集合。我怎么阻止这个?奇怪的是,如果我再次点击链接,它将运行该功能而不是原始主控制器。
var blogAppViewController = angular.module ('blogAppViewController', []);
blogAppViewController.controller('post_View_Ctrl', function ($scope, $http, $routeParams, $location) {
$scope._idList=[];
var globalPAGECount=0;
$http.get('/allposts').success(function(input_data) {
$scope.posts = input_data;
var posts_Length=$scope.posts.length;
$scope.post1=input_data[0];
$scope.post2=input_data[1];
$scope.post3=input_data[2];
$scope.post4=input_data[3];
$scope.post5=input_data[4];
$scope._idList.push({"Page":1, "_id":$scope.posts[0]._id});
var count=0;
var PageCount=2;
for (var each in $scope.posts){
count++;
if (count==5){
$scope._idList.push({"Page": PageCount, "_id": $scope.posts[each]._id});
count=0;
PageCount++;
}
}
$scope._idList.push({"Page": PageCount, "_id":$scope.posts[posts_Length-1]._id});
var listLength = $scope._idList.length;
// console.log($scope._idList[listLength-1]);
if($scope._idList[listLength-1]._id == $scope._idList[listLength-2]._id ){
$scope._idList.pop();
}
console.log($scope._idList);
$scope.a=globalPAGECount+1;
$scope.a_id=$scope._idList[globalPAGECount]['_id'];
$scope.b=globalPAGECount+2;
$scope.b_id=$scope._idList[globalPAGECount+1]['_id'];
$scope.c=globalPAGECount+3;
$scope.c_id=$scope._idList[globalPAGECount+2]['_id'];
$scope.d=globalPAGECount+4;
$scope.d_id=$scope._idList[globalPAGECount+3]['_id'];
$scope.e=globalPAGECount+5;
$scope.e_id=$scope._idList[globalPAGECount+4]['_id'];
});
$scope.get_post_Range = function(high,low) {
console.log(high, low);
console.log("At least enters here");
$http.get('/get_posts/high/' + high + '/low/' + low).success(function (returned) {
$scope.posts = returned;
console.log("success");
});
};
$scope.selectSearch = function(input) {
$scope.Search1 = input;
if ($scope.Search1 == 'Title'){
$scope.searchTitle = true;
$scope.searchContent = false;
$scope.search_Hits=null;
}
if ($scope.Search1 == 'Post Content'){
$scope.searchTitle = false;
$scope.searchContent = true;
$scope.search_Hits=null;
}
};
$scope.searchDatabase = function(type, input) {
if (type === 'Title') {
$http.post('/search_post_Titles', {title: input}).success(function (response) {
$scope.search_Hits = response;
});
}
if (type === 'Post Content') {
$http.post('/search_post_Content', {post: input}).success( function (response) {
$scope.search_Hits = response;
});
}
};
});
/////////// ///////////
var blogApp = angular.module('blogApp', [
'ngRoute',
'ngResource',
'blogAppViewController',
'blogSingleViewController',
'blognewEntryController',
]);
blogApp.config(['$routeProvider',
function ($routeProvider) {
$routeProvider.
when('/' , {
templateUrl: 'partials/all-posts.html',
controller: 'post_View_Ctrl'
}).
when('/post/:title' , {
templateUrl: 'partials/single-post.html',
controller: 'single_View_Ctrl'
}).
when('/get_posts/high/:high/low/:low' , {
templateUrl: 'partials/all-posts.html',
controller: 'post_View_Ctrl'
}).
when('/new_entry' , {
templateUrl: 'partials/new_Entry.html',
controller: 'entry_View_Ctrl'
}).
otherwise({
redirectTo: '/'
});
}]);
答案 0 :(得分:0)
控制器设计为每次导航更改只执行一次,因此没有明显的理由在此处执行两次。
也许您在$ routeProvider级别(如您所示)和html级别(请参阅this question)声明控制器
如果没有解决问题,这里将需要一名掠夺者。