Angular不显示$ scope数据,并在控制器中传递ng repeat。没有错误

时间:2016-03-10 08:30:51

标签: javascript php angularjs cordova ionic-framework

在这个项目中,我正在对离子的点击列表项执行查询。我从php json_encode获取数据。数据将显示在响应中的网络选项卡中。另外,我添加了$ scope.doctorList = {};之后写了这行$ scope.doctorList =来自成功函数的响应。数据也会显示在console.log($ scope.doctorList)中。

现在,当我尝试以角度显示此数据时,它不会显示任何内容。 我把它包含在ng-repeat中:ng-repeat =" doctorList中的医生"

语法似乎是正确的,因为同样的事情适用于另一个控制器,但在这里,我无法检索数据。页面变为空白,控制台/ netowrks选项卡中没有错误。

我正在为两个html文件使用一个控制器。请帮忙

这是路线文件

angular.module('app.routes', []).config(function ($stateProvider, $urlRouterProvider, $ionicConfigProvider) {
    $stateProvider.state('homeselect', {
        url: '/home-select',
        templateUrl: 'templates/homeselect.html',
        controller: 'homeCtrl'
    });

    $stateProvider.state('home', {
        url: '/home',
        templateUrl: 'templates/home.html',
        controller: 'homeCtrl'
    });
});

这是控制器

angular.module('app.controllers', []).controller('homeCtrl', function ($scope, $http, $ionicSideMenuDelegate, myServices, $window) {

    $ionicSideMenuDelegate.toggleLeft();

    $scope.loadDoc = function (type) {
        $http({
            url: "http://localhost/drmedic/retrieve_details_type.php",
            method: "POST",
            data: {
                data: type
            }
        }).success(function (response) {
            $scope.doctorList = {};
            $scope.doctorList = response;
            $window.location.href = '#/home-select';
        });

    };

    $http({method: 'GET', url: 'http://localhost/drmedic/retrieve_details.php'}).success(function (data) {
        $scope.contents = {};
        $scope.contents = data;
    });
});

这是ng-repeat

的html文件代码
<ion-list ng-repeat="doctors in doctorList">
  <ion-item>
  <center>
    {{doctors.name}}<br>
    {{doctors.fees}}
  </center>
  </ion-item>
</ion-list>

2 个答案:

答案 0 :(得分:1)

你可以使用service.Open一个名为service.js.的文件,然后注入app.js.我修改了代码如下:

Service.js:

 angular.module('app.services', [])
    .factory("AppService", function ($http) {
        var AppService= {};

        AppService.GetDetails = function (data) {
            return  $http({
            url: "http://localhost/drmedic/retrieve_details_type.php",
            method: "POST",
            data: {
                data: data
            }
            });
        return AppService;
        }

controller.js:

.controller('homeCtrl',function($scope,$http,$ionicSideMenuDelegate,myServices,$window,AppService) {
        $ionicSideMenuDelegate.toggleLeft();
        $scope.loadDoc = function(type){
       AppService.GetDetails({type:type}).success(function (response) {
              $scope.doctorList = {}; 
              $scope.doctorList = response;
              $window.location.href = '#/home-select';
        })
        .error(function (err) {
              console.log(err);
        });
   }
});

答案 1 :(得分:0)

ng-repeat遍历项目集合,创建自己的范围并在UI上呈现它。当我说集合时,它应该是一个数组。 https://docs.angularjs.org/api/ng/directive/ngRepeat

请检查doctorList的数据类型,我相信它是一个数组。

$scope.doctorList = []; 

我希望这可以解决问题,如果没有请分享代码我会仔细研究。