角度不更新范围更改时的模板
Angular not updating template on scope change
当输入框中的日期更改时,我正在尝试更新我的模板。
我的输入框 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>
我的表模板是:
<tr>
<td style="font-weight: bold">term</td>
<td ng-repeat="block in blocks">{{ data[block]['term'] }}</td>
<td>{{ total['term'] }}</td>
</tr>
最后我的控制器代码是:
$scope.blocks = ['A', 'B', 'C'];
$scope.data = {'A': {'term': 0, 'term2': 7}, 'B': {'term': 2, 'term2': 3}, 'C': {'term': 5, 'term2': 14}};
$scope.total = {'term': 50, 'term2': 70};
$scope.date = "2016-01-01";
$scope.sendPost = function() {
//console.log("working");
var post_data = {"date": $scope.date};
$http.post("/web_api", post_data).success(function (post_data, status) {
$scope.data = post_data.data;
$scope.total = post_data.total;
console.log($scope.data);
console.log($scope.total);
}).error(function(response) {
//console.log(response);
$scope.error = 'ERROR';
});
};
控制台日志在 post 请求中返回所需的对象。
每当你在 angularjs 之外执行某种形式的操作时,你需要让 angular 知道更新自身。
尝试添加 $scope.$apply();
之后$scope.data = post_data.data;
$scope.total = post_data.total;
在您的帖子请求中,它可能会起作用。
查看更多关于 $scope.$apply() 的信息
.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';
});
更好的是,使用新方式:
$http.post(...).then( onSuccess, onError );
在onSuccess
中,您可以使用 $scope.data = post_data.data;
$http.post(...).then(function(post_data){
$scope.data = post_data.data;
});
除此之外,您的代码工作正常。我彻底尝试了一下。
https://docs.angularjs.org/api/ng/service/$http#deprecation-notice
范围分配块包装在$timeout中,这会将操作发送到 que 的末尾。喜欢这个:
$http.post("/web_api", post_data).success(function (post_data, status) {
$timeout(function(){
$scope.data = post_data.data;
$scope.total = post_data.total;
console.log($scope.data);
console.log($scope.total);
});
}).error(function(response) {
//console.log(response);
$scope.error = 'ERROR';
});
你的$http承诺是不同的。检查 https://docs.angularjs.org/api/ng/service/$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 ?!
});
- ngDialog-弹出窗口未更新范围变量
- 无法使用编程加载的属性更新范围滑块.js
- 角度文件上传更新范围在完成项上
- AngularJS摘要周期偶尔无法更新范围
- Angular:从工厂更新范围不起作用
- NG模型对我来说是矫枉过正的.任何仅在单击按钮时更新范围的替代方案,而不是每次都更新范围
- Angularjs 可编辑的字体头不更新范围
- 通过选中复选框更新范围
- 如何更新范围值
- 事件的角度更新范围
- 角度不更新范围更改时的模板
- 更新范围时不调用 Angular 指令
- AngularJS更新范围$q
- 如何动态更新范围滑块
- 尝试使用Firebase facebookconnect和AngularJS更新范围变量时出现问题
- Angular ng src在更改图像源时不更新范围
- 在$index达到某个数字后更新a范围值
- Angular-当Factory数据发生更改时更新范围
- 在视图中使用compile in指令和ng repeat的组合会阻止正确更新范围元素列表
- AngularJS,在指令的传入内容内分配与更新范围时的不一致's的孤立范围