2017-08-07 29 views
0

控制器1得到國家PARAMS:無法從其它控制器

app.controller('task1Controller',['$scope', 'taskFactory', '$state', function($scope, taskFactory, $state){ 

    $scope.taskData = {}; 

    taskFactory.get().then(function(response){ 
     $scope.jsonData = response.data.data.resultCareGivers[0]; 
     $scope.taskData.fname = $scope.jsonData.firstName; 
     $scope.taskData.lname = $scope.jsonData.lastName; 
     $scope.taskData.email = $scope.jsonData.email; 
    }); 

    $scope.viewDetails = function(){ 
     console.log('taskData before route ', $scope.taskData); 
     $state.go('view-details', {some: $scope.taskData}); 
    }; 
}]); 

控制器2:

app.controller('viewDetailsController', ['$scope', '$stateParams', '$state', function($scope, $stateParams, $state){ 
    console.log('receiveing state params ', $stateParams); //returns null 
}]); 

HTML:

<div ng-controller="task1Controller"> 
    <div> 
     <a ng-click="viewDetails()">View Details</a> 
    </div> 
</div> 

<div class="container" id="main" ng-controller="viewDetailsController"></div> 

無法從控制器的一個得到國家PARAMS到控制器配備了兩個。我試圖發送state.go('params here')中的參數,我怎麼都無法在控制器2中檢索它(返回null)。

配置:

航線狀態提供配置

app.config(['$stateProvider', '$urlRouterProvider', 
    function($stateProvider, $urlRouterProvider){ 

    $urlRouterProvider.otherwise('task1'); 
    var header = { 
     templateUrl: 'views/header.html', 
     controller: function ($scope) { 
     } 
    }; 

    $stateProvider 
    .state('task1', { 
     url: "/task1", 
     views: { 
      header: header, 
      content: { 
       templateUrl: 'views/task1.html', 
       controller: function($scope){ 
        console.log('$scope1 ', $scope.image); 
       } 
      } 
     } 
    }) 
    .state('view-details', { 
     url: "/view-details", 
     views: { 
      header: header, 
      content: { 
       templateUrl: 'views/view-details.html', 
      } 
     } 
    }); 
}]); 

回答

1

您可以訪問使用$stateParams值。你必須參考使用$stateParams

控制器參數:

app.controller('viewDetailsController', ['$scope', '$stateParams', '$state', function($scope, $stateParams, $state){ 
    console.log('receiveing state params ', $stateParams.some); //returns some 
}]); 

配置

.state('view-details', { 
     url: "/view-details", 
     params: { some: null }, 
     views: { 
      header: header, 
      content: { 
       templateUrl: 'views/view-details.html', 
      } 
     } 

有關如何從控制器通過$stateParams傳遞一個對象到另一個更多信息,檢查Pass object as parameter in $state.go

+0

它說undefined當我使用$ stateParams.some – Satyadev

+0

你可以顯示你的配置文件? – Vivz

+0

'app = angular.module('techGeneTask',['ui.router']);' – Satyadev