2016-10-14 114 views
0

我試圖用angularjs和ionic.I更新我已經存在的條目。我有兩個看法,在第一個視圖中我列出了汽車名稱和它的型號名稱。當選擇汽車名稱時,它被引導到第二視圖,其中列出了關於選擇的汽車名稱及其型號的信息,以便它們被編輯和更新。我已經宣佈$rootScope.selectItem=key;,其幫助顯示選擇的汽車名稱。我在更新中遇到問題,因此,我需要幫助更新信息,方法是用舊信息替換新信息。如何更新離子

圖1:

<ion-list> 
     <ion-item ng-model="carBrand" ng-repeat="name in carSelect"> 
     <button ng-click="selectItem(name)">{{name.name}}</button>        
     <div class="item" ng-repeat="type in name.types">{{type}}</div> 
</ion-item> 
</ion-list> 

圖2:

<input type="text" ng-model="newName"><br> Select Type:{{selectItem}}</div> 
<ion-checkbox ng-repeat="field in fields" ng-model="field.checked" ng-checked="field.checked"> 
     {{field.name}} 
</ion-checkbox>   
<button ng-click="remove()">Delete</button> 
<button ng-click="Update()">Update</button></div> 

控制器:

carService.controller('carBrand', ['$scope', 'carRepository', '$rootScope', '$state', '$stateParams', function ($scope, carRepository, $rootScope, $state, $stateParams) { 
    $rootScope.carSelect = carRepository.data; 
    $scope.newCarList = []; 
    $scope.selectItem = function (key) { 
    $rootScope.selectItem=key; 
    $state.go('app.carModel'); 
    } 

    carService.controller('carAdd', ['$scope', '$rootScope', '$state', function ($scope, $rootScope, $state) { 
    $scope.newName = ""; 
    $scope.fields = [{ id: 1, name: 'Regular' }, 
    { id: 2, name: 'SUV' }, 
    { id: 3, name: 'Oversize' }, 
    { id: 4, name: 'Truck' }, 
    { id: 5, name: 'Motorcycle' }]; 

    $scope.sample = []; 
    $scope.Update = function() { 
    var carType = []; 
....} 

回答

0

如果我是你,我會改變一些事情,使之更加應用類。

首先,改變你的狀態路由:

$stateProvider.state("app.carModel", { 
    // .. 
    url: "edit/:id" 
} 

然後在您將selectedItem功能:

$scope.selectItem = function (car) { 
    // assuming your cars have an id property 
    // you can ofcourse also use something else 
    $state.go('app.carModel', { id: car.id }); 
} 

然後在你編輯/添加控制器:

carService.controller('carAdd', ['$scope', '$stateParams', 'carRepository', function ($scope, $stateParams, carRepository) { 
    var carId = $stateParams.id; 

    // make a copy, so it only gets changed in the repository when the user 
    // presses 'Update' 
    $scope.car = angular.copy(carRepository.get(carId)); 

    $scope.fields = [{ id: 1, name: 'Regular' }, 
     { id: 2, name: 'SUV' }, 
     { id: 3, name: 'Oversize' }, 
     { id: 4, name: 'Truck' }, 
     { id: 5, name: 'Motorcycle' } 
    ]; 

    $scope.sample = []; 
    $scope.Update = function() { 
     // .. 
     // in the save function replace the existing with the edited 
     carRepository.save($scope.car); 
    } 
}