gpt4 book ai didi

javascript - 使用工厂的 Angular 服务,数据正确但未显示在 UI 上

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

到目前为止这是 the complete source

angular
.module('app')
.factory('Friends', ['$http',function($http){
return {
get: function(){
return $http.get('api/friends.json')
.then(function(response){
alert(JSON.stringify( response.data));
return response.data;
});
}
};
}])

Controller :

angular.module("app")
.controller('homeCtrl',['$scope','Friends',
function($scope, Friends){
$scope.title = "Welcome";
$scope.items=["2016","2015", "2014"];
$scope.friends = Friends.get();
$scope.save = function(){
$http.post('/api/friends', friends);
}

}])

$stateProvider
.state('home',{
url:'/',
templateUrl:'templates/home.html',
controller: 'homeCtrl',
resolve : {
friends:['Friends', function(Friends){
return Friends.get();
}]
}
})

当我尝试提醒它时的结果:

enter image description here

用户界面空白: enter image description here

*我的导航栏 ddl 不起作用,该怎么办?

enter image description here

最佳答案

Friends.get() 返回一个 promise 。您必须使用 then 方法:

Friends.get().then(function(data) { $scope.friends = data; }); //instead of  $scope.friends = Friends.get();

关于javascript - 使用工厂的 Angular 服务,数据正确但未显示在 UI 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37207681/

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