虽然控制器/工厂拥有数据,但Angularjs查看空/不从$ scope中提取数据

时间:2016-07-12 01:18:42

标签: angularjs-scope angularjs-factory

我是Ionic& amp;的新手Angular并按照教程示例here

进行学习

在运行代码时,控制器&工厂似乎正确地获取数据,但项目是空的。我该如何解决?

HTML:

<ion-header-bar class="bar-stable">
    <h1 class="title">Page 2!</h1>
</ion-header-bar>
<ion-content class="padding">
    <p>Going to add playlist and video player here</p>
    <ion-list>
            <ion-item class="item-avatar" ng-repeat="item in users">
                <!--changed to ng-src to fix string problem-->
                <img ng-src="{{item.user.picture.thumbnail}}"/>
                <h2>{{item.user.name.first}} {{item.user.name.last}}</h2>
                <p>{{item.user.location.city}} {{item.user.password}}</p>
            </ion-item>
    </ion-list>
</ion-content>

控制器

添加$ scope.users = [];根据{{​​3}}

angular.module('starter.controllers', ['ionic','starter.services'])

.controller('MainCtrl',function(){
  console.log("Main Controller says: Hello World");
})

.controller('PlaylistCtrl',function($scope, userService){
    $scope.users=[];
    userService.getPlaylist().then(function(users){
        $scope.users = users;
        console.log($scope.users);
    });
})

服务

angular.module('starter.services', ['ionic'])

.factory('userService', function($http) {
    return {
        getPlaylist: function(){
            return $http.get('https://randomuser.me/api/?results=10').then(function(response){
                return response.data.results;
            });
        }
    }

})

以下是截图: here

1 个答案:

答案 0 :(得分:0)

嗨,根据图片,您的值直接在项目内,但在您提到的HTML中

 <img ng-src="{{item.user.picture.thumbnail}}"/> 

这是错误的,你的数组中没有用户对象,所以必须像这样直接调用键

<img ng-src="{{item.picture.thumbnail}}"/>

更改您的HTML

<ion-item class="item-avatar" ng-repeat="item in users">
                <!--changed to ng-src to fix string problem-->
                <img ng-src="{{item.picture.thumbnail}}"/>
                <h2>{{item.name.first}} {{item.name.last}}</h2>
                <p>{{item.location.city}} {{item.password}}</p>
            </ion-item>

enter image description here