0

我正在尝试根据$http结果从 json 中获取动态数据,但这对我不起作用。我认为问题在于回调。我的意思是当我$http继续运行时代码运行到 json 结果。这是我的 JavaScript 代码:

var operation_types = operation_types = [
    {nav_id: 1, nav_name: "Validation", nav_src: "validation", nav_href: "validation_list"},
    {nav_id: 2, nav_name: "Guests", nav_src: "guests", nav_href: "guests_list"}
];

angular.module("mainApp", ["kendo.directives"])
    .controller("HomepageCtrl", function ($scope,$http) {//Homepage
        /*receive user properties*/
        $http({
            url: 'API/v1/User/GetUserInfo',
            method: "GET",
            headers: { 'Content-Type': 'application/json' }
        }).success(function (data, status, headers, config) {
            if (data.Code != 1) {
                $scope.error = "Please enter valid username and password!";
            } else {
                console.log(data);
                if(data.viewGuest==true&&data.viewValidation==true){
                    $scope.source =operation_types;
                }else if(data.viewGuest==false){
                    source = operation_types[0];
                }else if(data.viewValidation==false){
                    source = operation_types[1];
                }

                //window.location.href = "homepage.html";
            }
        }).error(function (data, status, headers, config) {
            $scope.error = "There are problems with connection to server. Status:" + status + " Please, try to connect later.";
            $scope.validationClass = "invalid";
        });
         $scope.source =operation_types;

    })

这是我的 html 代码的相关片段(使用 kendo ui):

<kendo-mobile-list-view k-data-source="source">
            <div class="product" k-template>
                <a href="\#{{dataItem.nav_href}}">
                    <img src="images/{{dataItem.nav_src}}.jpg" alt="{{dataItem.nav_name}} image" class="pullImage"/>
                    <h3>{{dataItem.nav_name}}</h3>
                </a>
            </div>
        </kendo-mobile-list-view>

有人知道如何用角度来做吗?

4

1 回答 1

0

需要考虑的一些一般事项:

编辑

 if(data.viewGuest==true&&data.viewValidation==true){
                    $scope.source =operation_types;
                }else if(data.viewGuest==false){
                    source = operation_types[0];
                }else if(data.viewValidation==false){
                    source = operation_types[1];
                }

有时你在 $scope 上设置源

 $scope.source =operation_types;

和其他你设置一个名为 source 的局部变量。

source = operation_types[1];

你的意思是设置

$scope.source 

在最后 2 个“if”分支?

另一件事:

$http 调用是 asynchrounus 。因此,如果您要分配初始值

$scope.source 

那么最好在 ajax 调用之前完成,否则你会冒竞争条件的风险。

于 2015-03-03T11:35:02.263 回答