Javascript 范围更改时不更新模板

Javascript 范围更改时不更新模板,javascript,angularjs,Javascript,Angularjs,当输入框中的日期更改时,我正在尝试更新模板 我的输入框html是: <div class="col-md-4 pull-right"> <input type="text" id="id_date" class="form-control" ng-model="date" ng-change="sendPost()" value="{{ date }}"/> </div> 控制台日志将在post请求中返回所需的对象。每当您在angularjs之外执行

当输入框中的日期更改时,我正在尝试更新模板

我的输入框html是:

<div class="col-md-4 pull-right">
    <input type="text" id="id_date" class="form-control" ng-model="date" ng-change="sendPost()" value="{{ date }}"/>
</div>

控制台日志将在post请求中返回所需的对象。

每当您在angularjs之外执行某种形式的操作时,您需要通知angular更新自身

尝试添加一个$scope。$apply();之后

在您的post请求中,它可能会起作用


查看更多信息,请尝试在$timeout中包装作用域分配块,这将把操作发送到que的末尾。像这样:

$http.post(“/web\u api”,post\u数据)。成功(函数(post\u数据,状态){
$timeout(函数(){
$scope.data=post_data.data;
$scope.total=post_data.total;
log($scope.data);
log($scope.total);
});
}).错误(功能(响应){
//控制台日志(响应);
$scope.error='error';
});

.success
.error
已被弃用,但仍然有效。但是,它们给出了原始数据响应。 您正在尝试使用
response.data
,但没有这样的东西

// This is the old way. It still works, but you get a raw data response.
$http.post("/web_api", post_data).success(function (post_data, status) {
    $scope.data = post_data;  // <-- Fixed!
    $scope.total = post_data.total; // <-- There is no such thing.
    console.log($scope.data);
    console.log($scope.total);
}).error(function(response) {
    //console.log(response);
      $scope.error = 'ERROR';
  });
onSuccess
中,可以使用$scope.data=post_data.data

$http.post(...).then(function(post_data){
    $scope.data = post_data.data;
});
除此之外,您的代码工作正常。我彻底地试过了


您的$http承诺是不同的。检查

试试这个:

$http
.post('/web_api', post_data)
.then(function(response) {
    // success

    $scope.data = post_data.data;
    $scope.total = post_data.total;
    console.log($scope.data);
    console.log($scope.total);
}, function(response) {
    // error

    //console.log(response);
    $scope.error = 'ERROR';
})
.finally(function() {
    // finally.. like for disable loader ?! 
});

您可以显示从服务器获取的
post_data.data
的格式吗?post_data.data格式是{'A':{'term':1,'term2':7},'B':{'term':5,'term2':4},'C':{'term':7,'term2':16},并且您确定它是json,而不仅仅是包含json的字符串吗<代码>控制台.log(类型为$scope.data)类型为object。您的模板是否在另一个
控制器中?如果是这样,您可能希望使用
$scope.$parent.*
而不是
$scope.*
我在添加$scope时遇到$rootScope:inprog错误。$apply()检查最后一部分“诊断此错误”的链接:If(!$scope.$$phase){$scope.$apply();}将防止$rootScope:inprog错误这解决了medoesn的问题似乎没有什么区别。post_数据包含数据和items对象。然后分享您注入模板的方式。代码可以正常工作,因此您不必费心说明这是问题所在。console正在打印未定义的内容
// This is the old way. It still works, but you get a raw data response.
$http.post("/web_api", post_data).success(function (post_data, status) {
    $scope.data = post_data;  // <-- Fixed!
    $scope.total = post_data.total; // <-- There is no such thing.
    console.log($scope.data);
    console.log($scope.total);
}).error(function(response) {
    //console.log(response);
      $scope.error = 'ERROR';
  });
$http.post(...).then( onSuccess, onError );
$http.post(...).then(function(post_data){
    $scope.data = post_data.data;
});
$http
.post('/web_api', post_data)
.then(function(response) {
    // success

    $scope.data = post_data.data;
    $scope.total = post_data.total;
    console.log($scope.data);
    console.log($scope.total);
}, function(response) {
    // error

    //console.log(response);
    $scope.error = 'ERROR';
})
.finally(function() {
    // finally.. like for disable loader ?! 
});