【问题标题】:Angular js - cannot find scope data defined in controllerAngular js - 找不到控制器中定义的范围数据
【发布时间】:2015-06-19 14:49:52
【问题描述】:

请告诉您在控制器中完成所有操作后如何调用视图页面。目前我面临着 javascript 中的问题,我正在分配一个变量,该变量将在从控制器调用服务后出现,并且来自服务的数据将在一段时间后出现。

【问题讨论】:

  • 您能给我们更多信息吗?例如代码?
  • 就像说我想通过我当前的纬度来获取地图信息。

标签: angularjs angularjs-directive angularjs-scope


【解决方案1】:

我想从这个控制器获取地图 在一定的时间间隔后很长一段时间后来自我的纬度的信息但在视图页面中我正在获取地图信息但在几分之一秒后所以我在谷歌地图中面临问题因为我在视图页面中定义的变量在几分之一秒内未定义.我尝试通过在隐藏变量中设置值并获取数据,但脚本中定义的变量未定义。

frontApp.controller('mapController', function ($scope, $rootScope, services, $location, $window, $interval) {

    is_login(1);
    update();

    function update() {

        if (navigator.geolocation) {


            $window.navigator.geolocation.getCurrentPosition(function (position) {
                $scope.$apply(function () {

                    var latitude = position.coords.latitude;
                    var longitude = position.coords.longitude;


                    services.getMapData(latitude, longitude).then(function (data) {

                        $scope.mapdata = data.data;

                });
            });

        }
        else
        {
            window.location.href = site_url + "#/dashboard";
        }
    }

    $interval(update, 1000);
});

【讨论】:

  • 您应该使用这些新信息编辑您的原始问题
【解决方案2】:

我不是专家,所以我相信有更多经验的人会加入。我很确定您需要正确初始化模块。您的示例似乎没有定义任何依赖项(即 $scope、$location ...等)。

//you also need the app as well inorder to attach the controller to.

var frontApp = angular.module('mappingModule', []);

frontApp.controller('mapController',['$scope', '$rootscope','$location','$window', '$interval', function ($scope, $rootScope, services, $location, $window, $interval) {

// do things with your data here.
// i'm going to out on a limb and say you probably need to make sure you have a popup that 
// that prompts to user to share their location or navigator wont give it to you
}]);

此外,如果您希望模块运行,您需要链接到页面。需要更多代码和设置才能提供更好的答案。

您还需要定义服务。呃,我相信它被称为工厂,它负责处理需要在控制器依赖项数组中的依赖项中引用的内容。查看您的服务代码会有所帮助。

总体而言,您似乎没有将位置数据传递给控制器​​。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-23
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多