AngularJS如何通过$ rootScope获取数据到控制器? [英] AngularJS How to get data to controller via $rootScope?

查看:172
本文介绍了AngularJS如何通过$ rootScope获取数据到控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个移动应用程序和我在与获取数据到控制器的问题。我做的是我从服务中获得的数据和效果很好(在路由,这是'/'或根目录):

I'm working on an mobile app and I'm having problems with getting data to the controller. What i do is i get the data from a service and that works well (on routing this is '/' or root directory):

var listdnModule = angular.module('listdn', []);

listdnModule.factory('getActiveDnService', ['$http', function ($http) {
    return {
        getActiveDnSvc: function (izvajalecID) {
            return $http({ method: 'POST', url: 'svc.aspx/getActiveDN', data: "{'izvajalecID':" + "'" + izvajalecID + "'}", cache: true });
        }
    };
}]);

listdnModule.controller('listdnCtrl', ['$scope', '$http', 'getActiveDnService','$rootScope', function ($scope, $http, svc, $rootScope) {
    $scope.mjav = 1;
    svc.getActiveDnSvc($scope.mjav).success(function (data) {
        $rootScope.dnlist = data.d;
        $scope.listdn = data.d;
    });
}]);

所以这个工作正常。它集数据到rootScope和localscope和局部范围内读取它。

So this works fine. It sets the data to the rootScope and to localscope and reads it from local scope.

然后我连接的costumers costumerdetail名单。这条路线的设置是这样的:

Then i'm linking the list of Costumers to costumerdetail. The route settings are like this:

mSvc.config(['$routeProvider',
  function ($routeProvider) {
      $routeProvider.
        when('/', {
            templateUrl: 'tmpl/listdn.html',
            controller: 'listdnCtrl'
        }).
        when('/settings', {
            templateUrl: 'tmpl/nastavitve.html',
            controller: 'settings'
        }).
        when('/dndetail/:dnid', {
            templateUrl: 'tmpl/dndetail.html',
            controller: 'dndetailCtrl'
        }).
        otherwise({
            redirectTo: '/'
        });
  }]);

该错误后,来我试图让dndetail。我从$ routeParams正确的ID,但我不在正确的时间获得数据。在code是这样的:

The error comes after i try to get dndetail. I get the right ID from $routeParams but i dont get the data in the right time. The code is this:

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

dndetail.controller('dndetailCtrl', ['$rootScope', '$scope', '$routeParams', function ($rootScope, $scope, $routeParams) {
    console.log($rootScope.dnlist[0]);
    $scope.datal = $rootScope.dnlist;
    $scope.id = $routeParams.dnid;
    for(var i = 0; i <= datal.length; i++) {
        if (datal[i].ID === $scope.id) {
            $scope.data = datal[i];
        }
    }
}]);

这是错误我得到:

And this is the error i get:

正如你可以看到的console.log获取对象和打印输出(I掩盖它,因为它的公司数据),而数据1是不确定的。在Chrome DevTools特殊AngularJS标签还表示,数据是存在的:

As you can see the console.log gets the object and prints the output (I masked it because it's company data) while datal is undefined. The special AngularJS tab in chrome DevTools also says the data is there:

而对于年底的观点,这并不获取数据模板:

And for the end the template of the view that doesnt get data:

<ul ng-controller="dndetailCtrl" class="list-group">
    <li class="list-group-item">
        <div class="row info-row-li">
            <div class="pull-left">Naslov</div>
            <div class="pull-right">
                <a ng-href="https://maps.google.com/?q={{ data.Ulica }} {{ data.Posta }} {{ data.Kraj }}">
                    {{ $root.dnlist[1].Ulica }}<br />
                    {{ data.Posta }} {{ data.Kraj }}<br />
                    {{ data.Drzava }}</a>
            </div>
        </div>
    </li>
    <li class="list-group-item">
        <div class="row info-row-li">
            <div class="pull-left">Datum izvedbe</div>
            <div id="kupec-pe" class="pull-right">{{ data.DatumIzvedbe }}</div>
        </div>
    </li>
    <li class="list-group-item">
        <div class="row info-row-li">
            <div class="pull-left">Datum naloga</div>
            <div id="kupec-dst" class="pull-right">{{ data.DatumNaloga }}</div>
        </div>
    </li>
    <li class="list-group-item">
        <div class="row info-row-li">
            <div class="pull-left">Opis:</div>
            <div id="kupec-komerc" class="pull-right">{{ data.Opis }}</div>
        </div>
    </li>
    <li class="list-group-item">
        <div class="row info-row-li">
            <div class="pull-left">Šifra dejavnosti:</div>
            <div id="kupec-sif" class="pull-right">{{ data.sifDej }}</div>
        </div>
    </li>
    <li class="list-group-item">
        <div class="row info-row-li">
            <div class="pull-left">Šifra dejavnosti:</div>
            <div id="Div1" class="pull-right">{{ data.DatumIzvedbe }}</div>
        </div>
    </li>
</ul>

任何想法?

更新

这是新控制器:

dndetail.controller('dndetailCtrl', ['$rootScope', '$scope', '$routeParams', function ($rootScope, $scope, $routeParams) {
    $rootScope.dnlist;
    $scope.id = $routeParams.dnid;
        for (var i = 0; i <= $rootScope.dnlist.length; i++) {
            if ($rootScope.dnlist[i].ID === $scope.id) {
                $scope.data = $rootScope.dnlist[i];
            }
        }
}]);

还是不工作。从元素的数组我想通过ID来获得一个元素,然后将其保存到数据,以便我可以读取数据。 For循环犯规获得数据(unindentified dnlist),但如果我绑定{{$ root.dnlist [1] .Property}}在它工作的模板。

still doesnt work. From an array of elements i want to get one element by id and then save it to data so i can read from data. For loop doesnt get the data (unindentified dnlist) but if i bind {{ $root.dnlist[1].Property }} in the template it works.

推荐答案

如果任何人想知道的问题是与平等运营商。如果我改变===与检查类型==我不明白的类型错误之一:无法读取未定义错误的财产'ID'了。由于这是解决问题的办法,我想有一个解释也。我是一个JavaScript的初学者是真实的,如果有人有一个想法在这里发生了什么事,我非常想知道的解释。

If anyone was wondering the problem was with the equality operator. If I change === with the one that checks type == i dont get the TypeError: Cannot read property 'ID' of undefined error any more. Since this is the solution to the problem I'd like to have an explanation also. I'm a javascript beginner to be true and if someone got an idea what happened here, I'd very much like to know the explanation.

新控制器的工作原理:

dndetail.controller('dndetailCtrl', ['$rootScope', '$scope', '$routeParams', function ($rootScope, $scope, $routeParams) {
    $rootScope.dnlist;
    $scope.id = $routeParams.dnid;
        for (var i = 0; i <= $rootScope.dnlist.length; i++) {
            if ($rootScope.dnlist[i].ID == $scope.id) {
                $scope.data = $rootScope.dnlist[i];
            }
        }
}]);

这篇关于AngularJS如何通过$ rootScope获取数据到控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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