2016-05-01 114 views
1

我在AngularJS中的輸入字段有問題。無論我寫在一本書中,我在另一篇文章中獲得完全相同的文字(field_start = field_end)。我究竟做錯了什麼? ng-model有什麼問題嗎?顯示相同的輸入字段

template.html

<form name="editForm" role="form" novalidate ng-submit="vm.save()"> 
    <div class="modal-header"> 
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true" 
      ng-click="vm.clear()">&times;</button> 
    <h4 class="modal-title" id="myPackLabel">Create offer</h4> 
    </div> 
    <div class="modal-body"> 
    <jhi-alert-error></jhi-alert-error> 
    <div class="form-group"> 
     <label class="control-label" for="field_start">Miejsce startowe</label> 
     <input type="text" class="form-control" name="field_start" id="field_start" 
      ng-model="vm.transitPointStart.city" placeholder="np. Warszawa" 
      /> 
    </div> 
    <div class="form-group"> 
     <label class="control-label" for="field_end">Miejsce docelowe</label> 
     <input type="text" class="form-control" name="field_end" id="field_end" 
      ng-model="vm.transitPointEnd.city" placeholder="np. Wrocław" 
      /> 
    </div> 
    </div> 
    <div class="modal-footer"> 
    <button type="button" class="btn btn-default" data-dismiss="modal" ng-click="vm.clear()"> 
     <span class="glyphicon glyphicon-ban-circle"></span>&nbsp;<span>Cancel</span> 
    </button> 
    <button type="submit" ng-disabled="editForm.$invalid || isSaving" class="btn btn-primary"> 
     <span class="glyphicon glyphicon-save"></span>&nbsp;<span>Save</span> 
    </button> 
    </div> 
</form> 

controller.js

(function() { 
    'use strict'; 

    angular 
    .module('myApp') 
    .controller('AddOfferDialogController', AddOfferDialogController); 

    AddOfferDialogController.$inject = ['$scope', '$stateParams', '$uibModalInstance', 'entity', 'ShipmentOffer', 'TransitPoint', 'PackageSlot']; 

    function AddOfferDialogController ($scope, $stateParams, $uibModalInstance, entity, ShipmentOffer, TransitPoint) { 
    var vm = this; 
    vm.shipmentOffer = entity; 
    vm.transitPointStart = entity; 
    vm.transitPointEnd = entity; 

    vm.save = function() { 
     vm.shipmentOffer.date = new Date(); 
     TransitPoint.save(vm.transitPointStart); 
     TransitPoint.save(vm.transitPointEnd); 
     ShipmentOffer.save(vm.shipmentOffer); 
     $uibModalInstance.close(); 
     vm.isSaving = false; 
    } 

    vm.clear = function() { 
     $uibModalInstance.dismiss('cancel'); 
    }; 
    } 
})(); 
+0

這並不令人意外。您在兩種情況下填充entity.city屬性。你應該在這些屬性之間做出區別。您需要兩個屬性才能管理啓動城市和抵達城市。 – SayusiAndo

+0

謝謝你,我現在得到它,但它可以處理它,如果我需要兩個不同的entity.city在同一個對話框?我現在不能改變我的數據庫。 – ulou

+0

我不知道您的數據庫模式,但您可以隨意修改您在客戶端級別擁有的數據模型。之後,您需要注意使其適合您的數據庫模式。 – SayusiAndo

回答

2

您使用的是同一個對象在你的兩個參考:

vm.transitPointStart = entity; 
vm.transitPointEnd = entity; 

也許你需要創建一個CL一個在你的參考文獻中有不同的對象(這取決於你的要求):

vm.transitPointStart = Object.assign({}, entity); 
vm.transitPointEnd = Object.assign({}, entity); 
+0

這就是它,謝謝! – ulou

+0

我的榮幸..:D –

+0

我會感謝您的投票:D –