国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

angular.js - angular裡$.post無法綁定到$scope裡($.post、$http.post用法區(qū)別)
大家講道理
大家講道理 2017-05-15 16:52:49
0
3
605

如下所示,如果$scope.equipments=...那一段,放在$.post裡就不能綁定到$scope.equipments上,如果放在外面就可以,這是為什麼?


mainApp.controller('equipmentsController', function($scope, $http) { $.post("getAllDeviceList.action", {}, function(response){ $scope.equipments = [ { "id" : "1", "name" : "equipment01 ", "number" : "11" }, { "id" : "2", "name" : "equipment02 ", "number" : "22" }, { "id" : "3", "name" : "equipment03 ", "number" : "33" } ]; } ); $scope.equipments = [ { "id" : "1", "name" : "equipment01 ", "number" : "11" }, { "id" : "2", "name" : "equipment02 ", "number" : "22" }, { "id" : "3", "name" : "equipment03 ", "number" : "33" } ]; }
大家講道理
大家講道理

光陰似箭催人老,日月如移越少年。

全部回覆(3)
洪濤

經(jīng)過@lee1994522 的提醒,意識到如果用了$.post方法,那麼脫離了angular的上下文,所以無法綁定到angular的$scope裡。

this is the point,pls.. $.post is not an Angular issue and the stuff
it wraps is not in an Angular world,so it's obviously that the
equipments outside is in Angular's world and it works as you expect

try $scope.$apply() when you call a "none Angular" issue if you wanna
refresh sth

解決方法有兩個(gè):

$.post

第一個(gè)誠如@lee1994522所說,直接在$.post的回調(diào)函數(shù)的最後加上一句$scope.$apply(),把改變同步綁定到視圖上

$.post("xxx.action",
            {},
            function(response){
                if(response.result == "success"){
                    ...
                    }
                    $scope.equipments = equipments; 
                    $scope.$apply();
                }
            },
            "json"
    );

$http.post

AngularJS - Any way for $http.post to send request parameters instead of JSON

全域裡定義:


var app = angular.module('myApp'); app.config(function ($httpProvider) { $httpProvider.defaults.transformRequest = function(data){ if (data === undefined) { return data; } return $.param(data); } $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'; });

然後控制器裡面寫:

   $http.post("xxx.action").success(function(response) {  
            ...
            $scope.equipments = equipments; 
    });
曾經(jīng)蠟筆沒有小新

搞不懂樓主用$.post的意義。你注入$http又有何意義

給我你的懷抱

你的$.post不是angular的方法,所以實(shí)際上post的回調(diào)雖然執(zhí)行了,但angular在視圖上卻不知道這件事。你可以在$.post里的賦值操作后面再跟一句$scope.$apply();,那個(gè)賦值操作就生效了。

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板