AngularJS $ state.go在控制器之间丢失数据而不在模板

时间:2016-11-06 13:43:59

标签: angularjs ionic-framework

我只是从我的API获取信息,它反应良好。 console.log(response)将输出,

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

控制器

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');
        });
    }

现在我在我的视图中访问 vm.thisDoctor doctor-this.html

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

这是我的app.js

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

我的code错了吗?我已经工作了几个小时了。

1 个答案:

答案 0 :(得分:2)

从服务器获得响应后,您正在使用s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM) 更改视图,这就是您丢失数据的原因。相反,您可以将对象传递给状态并将其分配给控制器范围,如下所示。

<强> JS1

$state.go

<强> JS2

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
                }
            });
        });
    }

ApptsCtrl (注入.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 })

$stateParams