Drupal AngularJS ajax 简单的 ng-repeat 更新
Drupal AngularJS ajax simple ng-repeat update
我在更换 ng-repeat 时遇到问题
我的 html 在 ng-app
里面
<a href="#proceed" ng-click="order.submitOrderAjax()">
<?php print t('Finish order'); ?>
</a>
<ul>
<li ng-repeat="error in order.errors">{{ error.text }}</li>
</ul>
控制器app.js
app.controller('orderController', function() {
this.errors = [];
this.submitOrderAjax = function(){
var data = {} // some data here like name, phone
jQuery.ajax({
type: 'POST',
url: Drupal.settings.basePath + 'ajax/submit_cart',
data: { 'order': data },
dataType: 'json',
success: function(response){
if(response.status == true){
var link = response.link.replace(/\\//g, "/");
window.location.href = link;
}else{
this.errors = response.errors;
console.log(this.errors);
}
},
});
};
console.log returns 我需要但 ng-repeat 没有更新
实际上 html 在响应之前渲染所以你需要在响应之后渲染 ng-repeat
<a href="#proceed" ng-click="order.submitOrderAjax()">
<?php print t('Finish order'); ?>
</a>
<ul ng-if="flag">
<li ng-repeat="error in order.errors">{{ error.text }}</li>
</ul>
控制器
app.controller('orderController', function($scope) {
this.errors = [];
$scope.flag=false;
this.submitOrderAjax = function(){
var data = {} // some data here like name, phone
jQuery.ajax({
type: 'POST',
url: Drupal.settings.basePath + 'ajax/submit_cart',
data: { 'order': data },
dataType: 'json',
success: function(response){
if(response.status == true){
var link = response.link.replace(/\\//g, "/");
window.location.href = link;
}else{
this.errors = response.errors;
console.log(this.errors);
$scope.flag=true;
}
},
});
};
我在更换 ng-repeat 时遇到问题 我的 html 在 ng-app
里面<a href="#proceed" ng-click="order.submitOrderAjax()">
<?php print t('Finish order'); ?>
</a>
<ul>
<li ng-repeat="error in order.errors">{{ error.text }}</li>
</ul>
控制器app.js
app.controller('orderController', function() {
this.errors = [];
this.submitOrderAjax = function(){
var data = {} // some data here like name, phone
jQuery.ajax({
type: 'POST',
url: Drupal.settings.basePath + 'ajax/submit_cart',
data: { 'order': data },
dataType: 'json',
success: function(response){
if(response.status == true){
var link = response.link.replace(/\\//g, "/");
window.location.href = link;
}else{
this.errors = response.errors;
console.log(this.errors);
}
},
});
};
console.log returns 我需要但 ng-repeat 没有更新
实际上 html 在响应之前渲染所以你需要在响应之后渲染 ng-repeat
<a href="#proceed" ng-click="order.submitOrderAjax()">
<?php print t('Finish order'); ?>
</a>
<ul ng-if="flag">
<li ng-repeat="error in order.errors">{{ error.text }}</li>
</ul>
控制器
app.controller('orderController', function($scope) {
this.errors = [];
$scope.flag=false;
this.submitOrderAjax = function(){
var data = {} // some data here like name, phone
jQuery.ajax({
type: 'POST',
url: Drupal.settings.basePath + 'ajax/submit_cart',
data: { 'order': data },
dataType: 'json',
success: function(response){
if(response.status == true){
var link = response.link.replace(/\\//g, "/");
window.location.href = link;
}else{
this.errors = response.errors;
console.log(this.errors);
$scope.flag=true;
}
},
});
};