2015-08-28 96 views
1

我正在嘗試使用Angular JS來使用簡單患者數據庫的web api,其中每個患者都有一系列藥物。每種藥物和患者都有其自己的添加,編輯和列表操作。 當我嘗試編輯患者時,我添加了添加新葯的選項(這也應該是編輯患者條目的一種方式)。 所以我已經使用嵌套的狀態,其中患者編輯是父狀態的下一狀態的概念,而patientedit.Medications是孩子的狀態如下:在ui路由器父母狀態下使用解析

.state("patientEdit", { 
    abstract: true , 
    url: "/patient/edit/:PatID", 
    templateUrl: "app/patients/patientEditView.html", 
    controller: "PatientEditCtrl as vm", 
    resolve: { 
     patientResource: "patientResource", 

     patient: function (patientResource, $stateParams) { 

      console.log("the value of PatID is " + PatID); 
      console.log("Patient Edit is called ???") 
      return patientResource.get(
       { PatID: PatID }).$promise; 

     } 
    } 
}) 

.state("patientEdit.Medications", { 
    url: "/medications/:MedID/:PatID", 
    templateUrl: "app/patients/patientEditMedicationsView.html", 
    controller: "medicationEditCtrl as vm", 
    resolve: { 
     medicationResource: "medicationResource", 

     medication: function (medicationResource, $stateParams) { 
      var MedID = $stateParams.MedID; 
      var PatID = $stateParams.PatID; 
      console.log("the value of PatID is " + PatID); 
      console.log("the value of MedID is " + MedID); 
      console.log("Medication Edit is called ???"); 
      return medicationResource.getMedication(
       { MedID: MedID, PatID: PatID }).$promise; 

     } 
    } 
}) 

我過渡到建立藥品(這是在上面的代碼如下子狀態)的病人詳細視圖點擊按鈕時

<a class="btn btn-primary" 
    data-ui-sref="patientEdit.Medications({MedID: 0, PatID: vm.patient.ID})" 
    style="width:160px"> 
    Creat New Medicine 
</a> 

我有幾個職位在這裏堆過流和嵌套的狀態是子狀態會等到分辨率的文檔知的父狀態,也是我可以使用父狀態解析的父對象在子狀態和控制器中。 發生的事情是,我點擊新葯按鈕後,根據開發工具中的網絡標籤,父母狀態患者和孩子狀態藥物都已正確解決(根據開發工具網絡的響應標籤),但在任何地方都沒有轉換 。

編輯 這裏是我使用上述狀態的控制器: patientEditCtrl.JS:

(function() { 

angular.module("patientManagement") 
    .controller("PatientEditCtrl", 
       [ "patient", "$state", PatientEditCtrl]); 

function PatientEditCtrl(patient, $state) { 
    var vm = this; 
    // vm.title = "Editing"; 
    vm.patient = patient; 
    if (patient == null) 
    { 
     vm.title = "New medication"; 
    } 
    else { 
     console.log("here is the Parent controller of editing"); 
    if ( (vm.patient.ID)) { 
     console.log("here is the controller of editing"); 
     vm.title = "Edit: " + vm.patient.Name; 
    } 
    else { 
     console.log(vm.patient.Id); 
     vm.title = "New Patient"; 
    } 
    } 
    vm.submit = function() { 
     vm.patient.$save(); 
    } 
    vm.cancel = function() { 
     $state.go('patientList'); 
    } 

} 

}()); 

medicationEditCtrl.Js

(function() { 

    angular.module("patientManagement") 
     .controller("medicationEditCtrl", 
        ["medication", "$state", medicationEditCtrl]); 

    function medicationEditCtrl(medication, $state) { 
     var vm = this; 
     vm.medication = medication; 
     console.log("we are in medication edit CTRL"); 
     if ((vm.medication.MedID)) { 
      console.log("here is the controller of editing a medication "); 
      vm.title = "Edit: " + vm.medication.Name; 
     } 
     else { 
      console.log(vm.medication.MedID); 
      console.log("having a patientId of " + vm.medication.PatID); 
      vm.title = "New Medication"; 
     } 
     vm.submit = function() { 
      vm.medication.$save(); 
     } 
     vm.cancel = function() { 
      $state.go('patientList'); 
     } 



    } 

}()); 

回答

0

這裏的服務是我正在使用:

(function() { 

"use strict"; 
angular.module("common.services") 
     .factory("patientResource", ["$resource", patientResource]); 

function patientResource($resource) { 
    console.log("service is called"); 
    return $resource("http://localhost:49190/api/patients/:Id"); 

}; 

}()); 

當我使用(PatId)調用它,所以要求的解決辦法是改變服務調用名爲「patientEdit」狀態的決心如下:

return patientResource.get({ Id: PatID }).$promise; 
再次

感謝您的時間和回覆

相關問題