$scope.$on("$routeChangeSuccess", function () { if ($location.path().indexOf("/edit/") == 0) { var id = $routeParams["id"]; for (var i = 0; i < $scope.products.length; i++) { if ($scope.products[i].id == id) { $scope.currentProduct = $scope.products[i]; break; } } } });
$scope.refreshProducts = function () { $route.reload(); } }) .controller("editCtrl", function ($scope, $routeParams, $location) {
$scope.currentProduct = null;
if ($location.path().indexOf("/edit/") == 0) { var id = $routeParams["id"]; for (var i = 0; i < $scope.data.products.length; i++) { if ($scope.data.products[i].id == id) { $scope.currentProduct = $scope.data.products[i]; break; } } }
$scope.cancelEdit = function () { $location.path("/list"); }
$scope.updateProduct = function (product) { product.$save(); $location.path("/list"); }
$scope.saveEdit = function (product) { if (angular.isDefined(product.id)) { $scope.updateProduct(product); } else { $scope.createProduct(product); } $scope.currentProduct = {}; } });