2016-11-06 3 views
1

간단히 말해서 내 API에서 정보를 얻고 있으며 응답이 좋습니다. 내가보기에 vm.thisDoctor에 접근하고 이제

vm.getDoctor = function(id) { 
     $http.get(CONSTANTS.LINK+'/doctors/'+id).success(function(response) { 
      vm.thisDoctor = { 
       name : response.first_name + ' ' + response.last_name, 
       address : response.clinical_address, 
      }; 
      console.log(vm.thisDoctor.address); 
      $state.go('tabs.doctor'); 
     }); 
    } 

console.log(response) 의지 출력,

Object {id: 1, username: "testmd", first_name: "Juan", 
      last_name: "Dela Cruz", type: "Pediatrician"…} 

컨트롤러 의사 - this.html 여기 내 app.js

<a class="item item-thumbnail-left"> 
     <img src="cover.jpg"> 
     <h2>{{ appt.thisDoctor.name }}</h2> 
     <p>{{appt.thisDoctor.address}}</p> 
    </a> 

.state('tabs.doctor', { 
     url: '/doctor/{doctorId}', 
     views: { 
      'tab-appts': { 
      templateUrl: 'templates/appts/doctors-this.html', 
      controller: 'ApptsCtrl as appt' 
      } 
     } 
    }) 

code이 잘못 되었습니까? 나는 지금 몇 시간 동안이 일을 해왔다.

+0

? '$ state.go ('tabs.doctor');를 실행하면 논리 흐름이 잘못되었습니다. 데이터 인수가 없으므로 ApptsCtrl의 새 인스턴스가 생성됩니다. – charlietfl

+0

@charlietfl 통찰력 주셔서 감사합니다 – markhamknight

답변

2

서버에서 응답을 얻은 후 $state.go으로보기를 변경하면 데이터가 손실됩니다. 대신 객체를 상태에 전달하고 아래의 컨트롤러 범위에 다시 할당 할 수 있습니다.

JS1

vm.getDoctor = function(id) { 
     $http.get(CONSTANTS.LINK+'/doctors/'+id).success(function(response) { 
      vm.thisDoctor = { 
       name : response.first_name + ' ' + response.last_name, 
       address : response.clinical_address, 
      }; 
      console.log(vm.thisDoctor.address); 
      $state.go('tabs.doctor', { 
       args: { 
        data: vm.thisDoctor 
       } 
      }); 
     }); 
    } 

JS2

.state('tabs.doctor', { 
     url: '/doctor/{doctorId}', 
     views: { 
      'tab-appts': { 
      templateUrl: 'templates/appts/doctors-this.html', 
      controller: 'ApptsCtrl as appt' 
      } 
     }, 
     params: { args: {} } // Need to add this 
    }) 

ApptsCtrl ($stateParams 주입)은`vm.getDoctor` 어디에

vm.thisDoctor = ($stateParams.args || {}).data; 
+0

Bruv !! 정말 고맙습니다! 정말 도움이되었습니다 – markhamknight

+0

아니 upvote @markhamknight? – Antonis

+0

@Antonis 내 명성이 낮아서 내 업적이 반영되지 않으므로 내 게시물을 업 그레 이드 할 수 있습니까? – markhamknight

관련 문제