2

功能站菜单($范围){

$.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);

}

好像我在哪里做这个

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."}
    ];
}

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

4

2 回答 2

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."}
                                 ];

 });

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

于 2012-05-02T05:38:09.070 回答
0

你又来了..现在有很多关于这个的问题。
首先,我假设您放入 $scope.phones 的值是从 ajax 请求返回的并且没有硬编码,否则硬编码
jquery 中的 ajax 请求默认为异步的值没有任何意义。
因此,您需要对返回的数据执行的所有操作都需要 在示例success中的 so 事件内完成ajax request

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);

    }
于 2012-05-02T05:32:11.473 回答