热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如果它的内部jquery重复将无法工作-repeatwouldnotworkifitsinsidejquery

functionstationMenu($scope){$.ajax({url:usersstation_names_ajax,type:POST,

function stationMenu($scope){

$.ajax({
    url: "/users/station_names_ajax",
    type: "POST",

    success: function(data){

        $scope.phOnes= [
            {"name": "Nexus S",
                "snippet": "Fast just got faster with Nexus S."},
            {"name": "Motorola XOOM™ with Wi-Fi",
                "snippet": "The Next, Next Generation tablet."},
            {"name": "MOTOROLA XOOM™",
                "snippet": "The Next, Next Generation tablet."}
        ];

        //console.log(Stations); 
    }
});

// $scope.phOnes= Stations;
// console.log(Stations);

}

where as if I do this

好像我这样做

function stationMenu($scope){

    $scope.phOnes= [
        {"name": "Nexus S",
            "snippet": "Fast just got faster with Nexus S."},
        {"name": "Motorola XOOM™ with Wi-Fi",
            "snippet": "The Next, Next Generation tablet."},
        {"name": "MOTOROLA XOOM™",
            "snippet": "The Next, Next Generation tablet."}
    ];
}

it works....how can I make it work within ajax

它工作....我怎样才能使它在ajax中工作

2 个解决方案

#1


1  

function callService(){
    return  $.ajax({
    url: "/users/station_names_ajax",
    type: "POST",

    success: function(data){


        //console.log(Stations); 
    }
});
}
var $scope= {};

$.when(callService())
 .then(function(data){
           $scope.phOnes= [
                                   {"name": "Nexus S",
                                    "snippet": "Fast just got faster with Nexus S."},
                                   {"name": "Motorola XOOM™ with Wi-Fi",
                                    "snippet": "The Next, Next Generation tablet."},
                                   {"name": "MOTOROLA XOOM™",
                                    "snippet": "The Next, Next Generation tablet."}
                                 ];

 });

Use the when, then construct to work with the data returned from the server.

使用when,然后构造来处理从服务器返回的数据。

#2


0  

here you go again.. there are a lot of questions about this now.
first of all im assuming that the values you are putting in the $scope.phones is being returned from ajax request and isnt hardcoded otherwise it would make no meaning to hard code the values
the ajax request in jquery is async by default.
so everything you need to do with the data returned needs to be done inside the success event of the ajax request

so in your sample

你再来一次..现在有很多关于此的问题。首先我假设您放入$ scope.phones的值是从ajax请求返回的,并且不是硬编码的,否则硬编码默认情况下jquery中的ajax请求是async是没有意义的。因此,您需要对返回的数据执行的所有操作都需要在ajax请求的成功事件中完成,因此在您的示例中

function stationMenu($scope){

        $.ajax({
              url: "/users/station_names_ajax",
              type: "POST",

              success: function(data){


                  $scope.phOnes= [
                                   {"name": "Nexus S",
                                    "snippet": "Fast just got faster with Nexus S."},
                                   {"name": "Motorola XOOM™ with Wi-Fi",
                                    "snippet": "The Next, Next Generation tablet."},
                                   {"name": "MOTOROLA XOOM™",
                                    "snippet": "The Next, Next Generation tablet."}
                                 ];


                  //console.log(Stations);
//make use of anything returned and and $scope.phones here
              }
            });

//these lines wont work here
       // $scope.phOnes= Stations;
       // console.log(Stations);

    }

推荐阅读
author-avatar
sl51866
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有