0

I have an ng-click calling $scope.get_post_Range which is to return a range of database queries. The problem is that after it is completed, My original controller main function runs again ('post_View_Ctrl'). This returns the entire database collection. How do i stop this? The strange thing is that if I click the link again, it runs the function and not the original main controller.

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: '/'
                });
            }]);
seasick
  • 1,094
  • 2
  • 15
  • 29
  • When the URL is loaded into the browser with the query string - doing a refresh does not cause the controller to reload – seasick Jan 21 '14 at 12:13
  • This solved my problem - http://stackoverflow.com/questions/15472655/how-to-stop-angular-to-reload-when-address-changes It still doesn't make sense to me why the main controller is run. The route points to my custom high / low api. – seasick Jan 21 '14 at 13:04

1 Answers1

0

Controllers are design to execute only once per navigation change so there is no obvious reason to execute twice here.

Maybe you are declaring the controller both at $routeProvider level (as you shown) and at html level (see this question)

If it didn't solve the problem, a plunker will be needed here.

Community
  • 1
  • 1
bdavidxyz
  • 2,492
  • 1
  • 20
  • 40