AngularJS ngRepeat 不适用于 API 中的对象
AngularJS ngRepeat doesn't work with object from API
我正在尝试显示这个对象,就这么简单...但是当我重新加载页面时,没有任何反应,空白,这个对象是完美的,因为它在控制台中..
Angular
var store = angular.module('storeApp', []);
store.controller('CategoriesNProducts', ['$scope', function($scope) {
$scope.moltin = new Moltin({publicId: 'wA7eKb9jzqDvNI1V0HKIeGl9osh3FWcAKzOtZfcj'});
$scope.get_categories = function() {
$scope.moltin.Authenticate(function () {
$scope.moltin.Category.Tree({status: '1'}, function (tree) {
return tree;
});
});
};
$scope.categories = $scope.get_categories();
}]);
查看
<div ng-app="storeApp">
<h1>Store</h1>
<section id="categories" ng-controller="CategoriesNProducts">
{{categories.length}}
<div ng-repeat="(key, value) in categories">
<div id="{{value.description}}"></div>
{{ value }}
</div>
</section>
</div>
确定您的 Category.Tree
函数是异步的,因此,您不能分配 "async data" 并期望它像正常分配一样工作。您需要在异步函数回调中分配 $scope.categories
。
应该是
$scope.get_categories = function() {
$scope.moltin.Authenticate(function () {
$scope.moltin.Category.Tree({status: '1'}, function (tree) {
$scope.categories = tree;
$scope.apply(); // Use this if Category.Tree came from external lib
//Using apply, you tell to angularjs that $scope have been change by external lib
});
});
};
$scope.get_categories();
我正在尝试显示这个对象,就这么简单...但是当我重新加载页面时,没有任何反应,空白,这个对象是完美的,因为它在控制台中..
Angular
var store = angular.module('storeApp', []);
store.controller('CategoriesNProducts', ['$scope', function($scope) {
$scope.moltin = new Moltin({publicId: 'wA7eKb9jzqDvNI1V0HKIeGl9osh3FWcAKzOtZfcj'});
$scope.get_categories = function() {
$scope.moltin.Authenticate(function () {
$scope.moltin.Category.Tree({status: '1'}, function (tree) {
return tree;
});
});
};
$scope.categories = $scope.get_categories();
}]);
查看
<div ng-app="storeApp">
<h1>Store</h1>
<section id="categories" ng-controller="CategoriesNProducts">
{{categories.length}}
<div ng-repeat="(key, value) in categories">
<div id="{{value.description}}"></div>
{{ value }}
</div>
</section>
</div>
确定您的 Category.Tree
函数是异步的,因此,您不能分配 "async data" 并期望它像正常分配一样工作。您需要在异步函数回调中分配 $scope.categories
。
应该是
$scope.get_categories = function() {
$scope.moltin.Authenticate(function () {
$scope.moltin.Category.Tree({status: '1'}, function (tree) {
$scope.categories = tree;
$scope.apply(); // Use this if Category.Tree came from external lib
//Using apply, you tell to angularjs that $scope have been change by external lib
});
});
};
$scope.get_categories();