DWQA QuestionsCategory: Program$. Post cannot be bound to $scope in angular ($. Post, $http. Post usage difference)
sheilaCat asked 1 month ago

As shown below, if the $scope. Equipment =… Section is placed in $. Post, it cannot be bound to $scope. Equipment. If it is placed outside, why?

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 Answers
Best Answer
sheilaCat answered 1 month ago

after@lee1994522Realize that if the $. Post method is used, thenOut of the context of angular, so it cannot be bound to the 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

There are two solutions:
$.post
The first, as @ lee1994522 said, adds a sentence directly at the end of the callback function of $. Post$scope.$apply(), bind the change synchronization to the view

$.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
Overall definition:

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

Then the controller writes:

   $http.post("xxx.action").success(function(response) {  
            ...
            $scope.equipments = equipments; 
    });
fecoder answered 1 month ago

I don’t understand the meaning of $. Post. What’s the point of injecting $http

fecoder replied 1 month ago

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

fecoder replied 1 month ago

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

leftstick answered 1 month ago

Your$.postNoangularIn fact, the callback of post is executed, butangularI don’t know about it in the view. You can be there.$.postThe assignment operation in is followed by another sentence$scope.$apply();, the assignment takes effect.