在调用AJAX之后显示ng控制器的内容;作为“;作用
Displaying the contents of ng-controller after calling AJAX with the "as" function
调用AJAX(即$http
)后,我试图显示ng-repeat
的内容
<table ng-controller="TableController as tc">
<tr>
<th>Date</th>
<!-- other headers are here -->
</tr>
<tr ng-repeat="order in tc.orders" >
<td ng-bind="order.id"></td> //It doesn't appear
<td>@{{ order.id }}</td> //It doesn't appear as well. I use Laravel, so I need to put @
</tr>
</table>
以下是的相关脚本部分
angular.module('adminAngular', ['ui.bootstrap','dialogs.main'])
.controller('TableController', function(dialogs, $http){
var instance = this;
this.orders = [];
$http({
method : "POST",
url : "/admin/getOrders"
}).then(function (response) {
this.orders = response.data;
console.log("Inside; "+this.orders.length);
});
});
从console.log("Inside; "+this.orders.length)
中,我可以看到预期的数据被分配给了this.orders
数组。然而,正如这篇文章的标题所暗示的,数组并没有显示ng-repeat="order in tc.orders"
。
我关注这个问题,但关注这个问题并不能解决这个问题。现在我怀疑原因在于as
声明,我必须在这个场合使用它。
由于我在网上看不到太多关于as
的信息资源,如果你能给我任何建议,我将不胜感激。
您应该在promise解析函数中使用instance
分配给正确的实例(控制器实例):
angular.module('adminAngular', ['ui.bootstrap','dialogs.main'])
.controller('TableController', function(dialogs, $http){
var instance = this;
instance.orders = [];
$http({
method : "POST",
url : "/admin/getOrders"
}).then(function (response) {
instance.orders = response.data;
console.log("Inside; "+ instance.orders.length);
});
});
通常将此实例命名为:vm
指的是视图模型
示例:
angular.module('adminAngular', ['ui.bootstrap','dialogs.main'])
.controller('TableController', function(dialogs, $http){
var vm= this;
vm.orders = [];
$http({
method : "POST",
url : "/admin/getOrders"
}).then(function (response) {
vm.orders = response.data;
console.log("Inside; "+vm.orders.length);
});
});
相关文章:
- Gulp,浏览使用react作为CommonJS不起作用.React未定义
- 将对象作为参数传递时的 Javascript 函数作用域
- 如果图像具有 guid 作为名称,则 jqZoom 不起作用
- Angular js指令使用控制器作为语法ng点击不起作用
- SVG作为标记的图标在IE中不起作用
- 在调用AJAX之后显示ng控制器的内容;作为“;作用
- 如何更新作为属性传入的作用域变量
- 在Angular Directive中传递函数作为参数不会;不起作用
- PHP变量作为Javascript弹出窗口不起作用
- 我正在尝试一个接一个地播放一个 mp3 文件(一个作为介绍),但它不起作用.我该怎么做
- Highcharts在将javascript变量作为Hightcharts系列数据传递时不起作用
- 将字符串变量作为事件源 url 传递给 Fullcalendar 不起作用
- JS:为什么这个对象属性不起作用,只有当我把它作为变量放在其中一个方法中时,它才有效
- 在 Javascript 中,如果作为子窗口调用,则使用 focus() 设置的文本区域不起作用
- 将变量作为 JQuery CSS 选择器传递不起作用
- 作为触发器的 Jquery 输入类型文件不起作用
- 字符串和变量连接作为jQuery选择器不起作用.可能的原因是关闭
- 作为“名称”的Javascript数组变量不起作用
- ng show-使用服务作为作用域参数
- ionic ng-if不能在模板视图中将$index作为作用域的索引