ng从控制器函数重复调用

ng-repeat called from controller function

本文关键字:调用 函数 控制器 ng      更新时间:2023-09-26

我想用ng repeat写出应用程序中一些项目的列表,但我遇到了这个问题:

模板

<ion-slide-box  on-slide-changed="slideHasChanged($index)" ng-controller="AccountCtrl" ng-init="getData()">
    <ion-slide   ng-repeat="statitem in stats">
        <div class="box vh80">
            <h1>{{statitem.name}}</h1>
            <h1>Test</h1>
        </div>
    </ion-slide>
</ion-slide-box>

在AccountCtrl中

angular.module('starter.controllers', [])
    .controller('AccountCtrl', function($scope, Friends) {
        $scope.getData = function() {
            console.log('test');
            var friends = [
                { id: 0, name: 'Scruff McGruff' },
                { id: 1, name: 'G.I. Joe' },
                { id: 2, name: 'Miss Frizzle' },
                { id: 3, name: 'Ash efe' }
            ];
            $scope.stats = friends;
         }
   });

问题是,在模板中没有获得任何项目。

请问哪里会有问题?

谢谢你的帮助。

编辑:

我通过将方法调用移动到父div来解决这个问题。

<ion-content class="padding" ng-controller="AccountCtrl" ng-init="getData()">

尝试稍微清除模板以进行测试,以确保您的HTML是100%正确的,因此您可以这样做:

<div ng-controller="AccountCtrl" ng-init="getData()">
    <div ng-repeat="statitem in stats">
          <h1>{{statitem.name}}</h1>
    </div>
</div>

如果你看到任何结果,那么你必须检查你的HTML和CSS模板。

我通过将方法调用移动到父div来解决这个问题。

<ion-content class="padding" ng-controller="AccountCtrl" ng-init="getData()">