观看范围路线变更后的空 [英] watched scope is empty after route change

查看:78
本文介绍了观看范围路线变更后的空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我使用收集数据的3种不同的看法控制器。我看它2范围。
这是一个摘录:

I have a controller I'm using for 3 different views that collects data. I'm watching 2 scopes in it. This is an excerpt:

  // predefine place before Google Maps API GET
  $scope.place = {
    formatted_address: '',
    url: ''
  };

  // watch place in case of change (doesn't work otherwise)
  $scope.$watch('place', function () {
    $scope.poll.Poll.Location    = $scope.place.formatted_address;
    $scope.poll.Poll.UrlLocation = $scope.place.url;
  });

一切正常的观点1, $ scope.poll.Poll.Location $ scope.poll.Poll.Location 被充满了谷歌地图API的数据。

Everything works fine on view 1, the $scope.poll.Poll.Location and the $scope.poll.Poll.Location gets filled with data of the Google Maps api.

但是,当我点击一个按钮,它改变了我的路线(UI-SREF =视图2),这是使用相同的控制器,范围是空的。

But when I click on a button which changes my route (ui-sref="view2"), which is using the same controller, the scopes are empty.

我怎么能传输多个视图之间的观看范围具有相同的控制器?

How can I transfer the watched scopes between multiple views with the same controller?

推荐答案

如果您在使用相同的控制器多个状态,那么这两个国家得到它的控制器自己的实例。你必须通过数据作为参数到第二状态。

If you have multiple states using the same controller, then both states get it's own instance of the controller. You have to pass the data as a param to the second state.

这样的: UI-SREF =视图2({调查:调查})

不要忘了在参数添加到$ stateProvider状态定义。

Don't forget to add the param to the state definition in $stateProvider.

$stateProvider.state("homepageState", {
    url: "/home/",
    templateUrl: "templates/homePage.html",
    controller: "HomePageController",
    params: {
       poll: null //initial
    }
});

在你的控制器,你可以通过调用获得价值 $ stateParams.poll

In your controller you can get the value by calling $stateParams.poll

您还可以利用角度服务的。一个服务被实例化只有一次,可重复使用。

You also could make use of angular services. A service is instantiated only once and is reusable.

如果您需要等待数据来源不明,那么你必须看看的 Promise.when()

If you have to wait for data from an unknown source, then you have to take a look at Promise.when()

时(值[successCallback],[errorCallback],[progressCallback]);

包装一个对象可能是一个值或一个(第三方)当时能
  许成$ Q承诺。当你正在处理一个,这非常有用
  的对象可能是也可能不是一个承诺,或者承诺来
  从不能被信任的源

Wraps an object that might be a value or a (3rd party) then-able promise into a $q promise. This is useful when you are dealing with an object that might or might not be a promise, or if the promise comes from a source that can't be trusted.

这篇关于观看范围路线变更后的空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆