获取错误以通过$ state.go传递参数

时间:2017-08-09 17:56:49

标签: angularjs parameters state.go

我是角度js的新手。我在使用$state.go时遇到了一些问题。我的HeaderController中有一个文本字段和一个按钮。在这个按钮中,我在ng-click中调用了一个函数。我想在这个按钮点击上清除一个数组,并希望使用参数转到SearchController。其中我将文本字段值作为参数。我的数组使用该函数清除,但是对于状态更改,我收到错误

angular.js:9419 Error: Could not resolve 'search' from state 'home'
    at Object.y.transitionTo (angular-ui-router.min.js:7)
    at Object.y.go (angular-ui-router.min.js:7)
    at Scope.$scope.searchFn (headerControllers.js:75)
    at angular.js:10204
    at angular.js:17971
    at Scope.$eval (angular.js:11961)
    at Scope.$apply (angular.js:12061)
    at HTMLButtonElement.<anonymous> (angular.js:17970)
    at angular.js:2613
    at forEach (angular.js:310)

我有配置代码

 angular.module('movieApp').config(['$urlRouterProvider','$stateProvider', '$locationProvider', function($urlRouterProvider,$stateProvider,$locationProvider){
        $stateProvider.state('home',{
            url : '/home',
            templateUrl : 'app/components/home/home.html',
            controller : 'HomeController',
        }).state('search.id',{
            url : '/search',
            params: {
                param1: null
            },
            templateUrl : 'app/components/search/search.html',  
            controller : 'SearchController',    
        }).state('header',{
            template : '<main-header></main-header>',           
        }).state('category',{
            url : '/category/:id',
            templateUrl : 'app/components/category/category.html',  
            controller : 'CategoryController',  
        }).state('yearsort',{
            url : '/yearsort/:time_id/:page_id',
            template : '<year-sort></year-sort>',   
        }).state('budgetsort',{
            url : '/budgetsort/:price_id/:page_id',
            template : '<budget-sort></budget-sort>',   
        });
        $urlRouterProvider.otherwise('home'); 
}]);

我的标题控制器是

angular.module('movieApp.header.controller', []).controller('HeaderController', ['$scope','HomeFactory','$timeout','$state',function($scope,HomeFactory,$timeout,$state){

    $scope.header = "home page";
    $scope.categoryList = [];

    var callApi = function(){
                var apidata = localStorage.getItem('apiData');
                apidata = JSON.parse(apidata);
                $scope.allMovieList = apidata;
                movieCatgFn(apidata);
                searchList(apidata);
    }
    var allMovieList = callApi();
    function movieCatgFn(data){
        var splitToAry = [];
        for(var i=0; i<data.length; i++){
            splitToAry.push(data[i].genres.split("|"));
        }
        var fullArray = [];
        $scope.genersAry = [];
        fullArray=splitToAry.join().split(',');
        for(var j=0; j<fullArray.length; j++){
            var count = 0;
            $scope.genersAry.push(fullArray[j]);
            for(var k=0; k<$scope.genersAry.length; k++){
                if(fullArray[j]==$scope.genersAry[k]){
                    count+=1;
                }
                if(count>1){
                    $scope.genersAry.pop();
                }
            }
        }   
    }

    function searchList(data){
        $scope.changeFn = function(userData){
            if(userData!=null || userData!= ""){
                $scope.searchCatgAry = [];
                for(var i=0; i<data.length; i++){
                    var str = data[i].movie_title.toLowerCase().trim();
                    var search = userData.toLowerCase().trim();
                    if(str.search(search) != -1){
                        $scope.searchCatgAry.push(data[i].movie_title);
                    }
                }
                if(userData==null || userData== ""){
                    $scope.searchCatgAry = [];
                }
            }
        }
    }

    $scope.serchAutocatg = function(data){
        console.log(data);
        $scope.searchName = data;
        $scope.searchCatgAry = [];
    }

     $scope.searchFn = function(searchName){
        console.log('hello: '+searchName);
        $scope.searchCatgAry = [];
        $state.go('search',{param1: searchName});
    }    

 }]);

标题html是

<header ng-controller="HeaderController"> 
<form class="navbar-form navbar-right">
  <div class="form-group">
    <input type="text" class="form-control" placeholder="Search" ng-model="searchName" ng-change="changeFn(searchName)">
  </div>
<button type="button" class="btn btn-default" ng-click="searchFn(searchName)">Submit</button>
</form>

我的搜索控制器是

angular.module('movieApp.search.controller', []).controller('SearchController', ['$scope','movieAPI','HomeFactory','$timeout','$state',function($scope,movieAPI,HomeFactory,$timeout,$state) {

    $scope.searchName = $state.params.searchId;
    $scope.searchCatgAry = [];

    var callApi = function(){
                var apidata = localStorage.getItem('apiData');
                apidata = JSON.parse(apidata);
                $scope.allMovieList = apidata;
                searchFieldFn(apidata);
    }
    var allMovieList = callApi();

    function searchFieldFn(data){
        for(var i=0; i<data.length; i++){
            var str = data[i].movie_title.toLowerCase().trim();
            var search = $scope.searchName.toLowerCase().trim();
            console.log(search);
            if(str.search(search) != -1){
                $scope.searchCatgAry.push(data[i]);
            }
        }
        console.log($scope.searchCatgAry);
        $scope.searchName = "";
        console.log('$scope.searchName: '+$scope.searchName);
    }



 }]);

1 个答案:

答案 0 :(得分:0)

问题已经解决。 config.js,标题控制器和搜索控制器中的变化很少。

在config.js中,我必须将状态写为:

.state('search',{
        url : '/search/:id',
        templateUrl : 'app/components/search/search.html',  
        controller : 'SearchController',    
    })

在$ state.go的标题控制器中,我必须写

 $scope.searchFn = function(searchName){
    $state.go('search',{id: searchName},{reload:true});
}

在搜索控制器中,我必须编写代码来访问参数

$scope.searchName = $state.params.id;