2014-01-21 92 views
0

我有一個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

當使用查詢字符串將URL加載到瀏覽器時 - 執行刷新不會導致控制器重新加載 – seasick

+0

這解決了我的問題 - http://stackoverflow.com/questions/15472655/how-to-stop-角度重新加載時,地址更改它仍然沒有任何意義,爲什麼主控制器運行。路線指向我的自定義高/低api。 – seasick

回答

0

控制器是設計,所以每導航改變只執行一次沒有明顯的理由在這裏執行兩次。

也許你都在$ routeProvider層次上聲明控制器(如你所示),並在HTML水平(見this question

如果不解決這個問題,將在這裏需要plunker。

+0

我有這個問題,我在一個點上從html中刪除它。 – seasick

+0

它解決了問題嗎? – bdavidxyz

+0

不是這個問題,但它確實從控制器加載兩次 – seasick

相關問題