2016-07-25 41 views
0

這是我的初始模塊:來自控制器抓取ID和添加到另一個控制器API調用

angular.module('ngApp', []) 
.factory('authInterceptor', authInterceptor) 
.constant('API', 'http://myserver.com/ecar/api') 
.controller('task', taskData) 

function taskData($scope, $http, API) { 
    $http.get(API + '/tasks'). 
    success(function(data) { 
    $scope.mainTask = data; 
    console.log(data); 
    }); 
} 

這將返回一個JSON陣列,其中包含一個鍵「CAR ID:1297」在每個項目或任何陣列。我需要抓住該ID並以某種方式將其存儲在變量中,然後在另一個控制器中調用它。原因是,當這輛車顯示較小的信息時,他們可以點擊「查看詳細信息」,它會打開另一個頁面,顯示基於ID的汽車的所有細節。

也許是這樣的:

angular.module('ngApp', []) 
.factory('authInterceptor', authInterceptor) 
.constant('API', 'http://myserver.com/ecar/api') 
.controller('task', taskData) 
.controller('carDetails', carDetails) 

function taskData($scope, $http, API) { 
    $http.get(API + '/tasks'). 
    success(function(data) { 
    $scope.mainTask = data; 
    console.log(data); 
    }); 
} 

function carDetails($scope, $http, API) { 
    $http.get(API + '/car/$id'). 
    success(function(data) { 
    $scope.mainTask = data; 
    console.log(data); 
    }); 
} 

我只需要從該JSON發送「taskData」功能搶「CAR ID」,並將其添加到「carDetails」功能的API調用(如你往上看)。所以我需要顯然創建一個變量(如$ id)傳遞給第二個函數,但我不知道如何。

任何人都可以幫助我嗎?

回答

0

得到這個基於對這個職位的工作 - Inserting JSON ID key into ng-click directive and then pass that into another controller

這裏是工作的腳本:

ngular.module('ngApp', []) 
.service('CarDetailService', CarDetailService) 
.controller('task', taskData) 
.controller('carDetails', carDetails); 

CarDetailService.$inject = ['$http', 'API']; 
function CarDetailService($http, API) { 
    var CarDetailService = this; 
    CarDetailService.setCar = function(carId) { 
    CarDetailService.carId = carId; 
    }; 

    CarDetailService.getCar = function() { 
    return $http.get(API + "/car/" + CarDetailService.carId); 
    }; 
} 

taskData.$inject = ['$scope', '$http', 'API', 'CarDetailService']; 
function taskData($scope, $http, API, CarDetailService) { 
    $http.get(API + '/tasks'). 
    success(function(data) { 
    $scope.mainTask = data; 
    console.log(data); 
    }); 

    $scope.selectCar = function(carId) { 
    CarDetailService.setCar(carId); 
    $location.url('/car-details-route'); //go to details page 
    }; 
} 

carDetails.$inject = ['$scope', 'CarDetailService']; 
function carDetails($scope, CarDetailService) { 
    CarDetailService.getCar().success(function(details) { 
    $scope.details = details; 
    }); 
} 
0

您可以使用Angular Services跨兩個控制器共享數據。

angular.module('ngApp', []) 
.factory('authInterceptor', authInterceptor) 
.constant('API', 'http://myserver.com/ecar/api') 
.controller('task', taskData) 
.service('carService', carService) 
.controller('carDetails', carDetails) 

function taskData($scope, $http, API, carService) { 
    $http.get(API + '/tasks'). 
    success(function(data) { 
    $scope.mainTask = data; 
    console.log(data); 
    }); 
    // Put a click listener for Car, suppose it takes in the id as the parameter 
    $scope.carClickListener = function(data) { 
     carService.setSelectedCarId(data); 
    } 
} 

// Use to service to store the selected Car Id 
function carService() { 
    var selectedCarId = ""; 
    this.setSelectedCarId = function(id) { 
     selectedCarId = id; 
    } 
    this.getSelectedCarId = function() { 
     return selectedCarId; 
    } 
} 
function carDetails($scope, $http, API) { 
    $http.get(API + '/car/$id'). 
    success(function(data) { 
    $scope.mainTask = data; 
    console.log(data); 
    }); 
} 
0

您可以使用一個簡單的value來做到這一點。首先創建value

angular.module('ngApp').value('carValue', { id: '' }); 

然後你注入該值到控制器中(S):

function taskData($scope, $http, API, carValue) { 
    [...] 
    carValue.id = 'someValue'; <-- set this however you need to 
    [...] 
} 

function carDetails($scope, $http, API, carValue) { 
    $http.get(API + '/car/' + carValue.id) 
     .success(function(data) { 
      $scope.mainTask = data; 
      console.log(data); 
     }); 
} 
相關問題