gpt4 book ai didi

javascript - $http 在 Angular 1.4.8 中使用 Rest Controller

转载 作者:行者123 更新时间:2023-11-28 18:36:11 25 4
gpt4 key购买 nike

我最近是 AngularJS 的新手,我正在尝试在我的 controller 上使用别名来调用来自 $http 响应的数据 工厂

我现在所做的如下:

DiscoverService.js

var discoverService = angular.module('discover.services', []);

discoverService.factory('discoverV', function ($http) {
return {
list: function(callback){
$http.get('https://myurl.com/get/jsondata').success(callback);
}
};
});

发现Ctrl.js

var discoverModule = angular.module('discover', ['discover.services']);

discoverModule.controller('discoverCtrl', [ '$scope', 'discoverV', function($scope, discoverV) {
$scope.elm = [];

discoverV.list(function(data) {
$scope.elm = data;
});
}]);

_discover.html

<div class="wrapper" ng-controller="discoverCtrl as discover">
<article>
<section id="block-stories" class="container" ng-repeat="element in elm">
{{element.story_id}}
</section>
</article>
</div>

我一直在尝试这样做,但我不明白为什么下一个代码不起作用:

discoverModule.controller('discoverCtrl', [ 'discoverV', function(discoverV) {
var elm = this;
elm.videos = [];

discoverV.list(function(data) {
elm.videos = data;
});

}]);

<div class="wrapper" ng-controller="discoverCtrl as discover">
<article>
<section id="block-stories" class="container" ng-repeat="element in discover.videos">
{{element.story_id}}
</section>
</article>
</div>

我做错了什么?

感谢您的建议。

最佳答案

一旦将 discoverCtrl 编写为 discovery,您就应该使用 discover 来访问 Controller 范围。

所以,你应该写这样的内容:

<div class="wrapper" ng-controller="discoverCtrl as discover">
<article>
<section id="block-stories" class="container" ng-repeat="element in discover.videos">
{{element.story_id}}
</section>
</article>
</div>

我创建了一个示例 ( link ),其中我只是模拟了 $http 调用并且它有效(仅当您遇到另一个错误时)。

希望能有所帮助。

关于javascript - $http 在 Angular 1.4.8 中使用 Rest Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37038802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com