2016-04-05 21 views
0

Denetleyiciler içinde bir hizmeti iletmeye çalışırken küçük bir sorun yaşıyorum.Köşeli - bir denetleyiciden diğerine bilgi geçmek

Yapmaya çalıştığım bir alışveriş sepeti, bir öğe listem var ve bir düğmeye bastığımda, bu öğeler sepete eklendiğinde, o öğeleri sepette ayrı olarak listelemek istiyorum sayfa ayrı bir denetleyici kullanıyor, bu yüzden sepet için bir fabrika kullanmaya çalışıyorum, ancak bir denetleyici içinde bir fabrika nesnesini ayarlayabileceğinizi bilmiyorum.

İşte kodum, bana doğru yönde işaret edebilirsiniz umuyoruz.

var app = angular.module("Shop", []); 

app.factory('DataService', function() { 
var cart = []; 
var set = function (data) { 
    cart = data; 
} 
var get = function() { 
    return cart; 
} 
}); 

app.controller("catalogController", function ($scope, $http) { 
$scope.bookStore = { 
    selected: {}, 
    books: null 
}; 
$scope.cart = []; 
$http.get("json/books.json") 
    .success(function (data) { 
     console.log(data); 
     $scope.bookStore.books = data; 
    }) 
    .error(function (err) { 

    }); 

$scope.addToCart = function (book) { 
    var found = false; 
    $scope.cart.forEach(function (item) { 
     if (item.id === book.id) { 
      item.quantity++; 
      found = true; 
     } 
    }); 
    if (!found) { 
     $scope.cart.push(angular.extend({ 
      quantity: 1 
     }, book)); 
    } 
}; 

$scope.removeFromCart = function (item) { 
    var index = $scope.cart.indexOf(item); 
    $scope.cart.splice(index, 1); 
}; 

$scope.getCartPrice = function() { 
    var total = 0; 
    $scope.cart.forEach(function (product) { 
     total += product.price * product.quantity; 
    }); 
    return total; 
}; 
}); 

app.controller("checkoutController", function ($scope, DataService) { 
$scope.cart = DataService; 
}); 
+0

Olası yinelenen: http://stackoverflow.com/questions/20181323/passing-data-between-controllers-in-angular-js – Roy

cevap

1

Değişim şeyler gibi bir şey biraz: o zaman

app.factory('DataService', function() { 
    var cart = []; 

    return { 
     set: function (data) { 
      cart = data; 
     }, 
     get: function() { 
      return cart; 
     }, 
     add: function (item) { 
      cart.push(item); 
     } 
    } 
}); 

... 

app.controller("checkoutController", function ($scope, DataService) { 
    $scope.cart = DataService.get(); 
}); 

Ve fabrikada fonksiyonlara diğer denetleyiciye de kartta $http.get yöntemi ve tüm operasyonları taşımak ve aynı bunları beyan böyle bir şey yapması gerektiğini yukarıda Dataservice.get()

1

olarak yol: bir hizmet açısal js bir tekil olduğu

, o benim uygulamanızda bu sınıfın yalnızca bir örneğiniz var. senin denetleyicileri içinde Sonra

var app = angular.module("Shop", []); 

    app.factory('DataService', function ($http) { // usualy your service is the one which call your API (not your controller) 
    var cart = null; // the cart array is set in the instance of the class as private 

    return{ // here you declare all the functions you want to call from outside (your controllers) 
      set : function (data) { 
       cart = data; 
      }, 
       get: function(){ 
       return cart; 
      }, 
      getFromAPI = function() { // the code you have in your controller should goes here 
       return $http.get("json/books.json") 
         .success(function (data) { 
          console.log(data); 
         cart = data; //now you set you cart variable 
         }) 
         .error(function (err) { 

        }); 
      }, 
     }); 

:

app.controller("catalogController", function ($scope, DataService) { // include your service as a dependency 
$scope.bookStore = { 
    selected: {}, 
    books: null 
}; 
$scope.cartInCatalogController = DataService.get(); // it will set the value of cart that's in your service to your controller's scope 
if(!$scope.cartInCatalogController) {// if it's null so call the API 
     DataService.getFromAPI()// this function should return a promise 
     .success(function(data){// so call the success function 
      $scope.cartInCatalogController = data; 
     }) 
     .error(function(error){ 
      // do something here if you want 
     }); 
}); 

Sen diğer denetleyici içinde aynısını yapabilirsiniz. addToCard işlevi ve diğer şeyleri kendiniz bulmanızı sağlıyorum.

buradan başlayabilirsiniz :)

ait
İlgili konular