【发布时间】:2016-06-23 07:46:49
【问题描述】:
我有一个角度工厂和控制器:
angular.module('myApp', ['ui.router', 'ngResource'])
.factory('Widget', ['$http', function WidgetFactory($http) {
return {
all: function() {
return $http({method: "GET", url: "/widgets"});
}
};
}])
.controller('StoreCtrl', ['$scope', 'Widget', function($scope, Widget) {
$scope.widgets = Widget.all();
}]);
在我的前端我有
<div ng-controller="StoreCtrl">
<li ng-repeat="widget in widgets">
{{ widget.price }}
{{ widget.name }}
</li>
</div>
但是我的{{ widget.price }} 等中没有填充任何内容。
我错过了什么?
【问题讨论】:
-
在你的控制器中打印 $scope.widgets 检查它是否包含数据
-
如果数据可用,您可以通过使用此行 console.log($scope.widgets); 查看您的数据或在浏览器中使用调试模式。
-
有可用数据。它是一个对象数组。但是对象属性没有出现在它们的
{{ widget.name }}中的ng-repeat中。 -
这不是一个好习惯,但现在代替 $scope 使用 $rootScope 并尝试。
-
@VishalSingh 完全打破了它。
标签: javascript angularjs ajax angularjs-service angular-controller