论证' PeopleController'不是一个函数,得到了未定义的IonicJS

时间:2016-03-29 22:49:35

标签: javascript angularjs ionic-framework

  1. 使用IonicJS并渲染一些json细节。
  2. 收到错误:"参数' PeopleController'不是一个功能,     未定义" 我已检查过该文件 people_controller.js
  3. (function(){     var app = angular.module(' peopleController',[]);

    app.controller('PeopleController',
        function($scope, $http) {
        var url = 'http://localhost:3000/api/people';
        $http.get(url)
        .success(function(people) {
        $scope.people = people;
        })
        .error(function(data) {
        console.log('server side error occurred.'); 
        }); 
        } 
        );
        }); 
    

    app.js:

    angular.module('starter', ['ionic', 'peopleController'])
    
    .run(function($ionicPlatform) {
      $ionicPlatform.ready(function() {
        if(window.cordova && window.cordova.plugins.Keyboard) {
          // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
          // for form inputs)
          cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    
          // Don't remove this line unless you know what you are doing. It stops the viewport
          // from snapping when text inputs are focused. Ionic handles this internally for
          // a much nicer keyboard experience.
          cordova.plugins.Keyboard.disableScroll(true);
        }
        if(window.StatusBar) {
          StatusBar.styleDefault();
        }
      });
    })
    

    的index.html:

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
        <title></title>
    
        <link href="lib/ionic/css/ionic.css" rel="stylesheet">
        <link href="css/style.css" rel="stylesheet">
    
        <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
        <link href="css/ionic.app.css" rel="stylesheet">
        -->
    
        <!-- ionic/angularjs js -->
        <script src="lib/ionic/js/ionic.bundle.js"></script>
    
    
        <!-- cordova script (this will be a 404 during development) -->
        <script src="cordova.js"></script>
    
        <!-- your app's js -->
        <script src="js/app.js"></script>
      </head>
       <body ng-app="starter">
    
        <ion-pane>
          <ion-header-bar class="bar-stable">
            <h1 class="title">Ionic Blank Starter</h1>
          </ion-header-bar>
          <ion-content>
          <ul class="list" ng-controller="PeopleController">
    <li class="item" ng-repeat="person in people">
    <h3>{{person.name}}</h3>
    
    </li>
    </ul>
    
          </ion-content>
        </ion-pane>
         <script src="../js/controllers/people_controller.js"></script>
    
      </body>
    </html>
    

    我做错了什么?

3 个答案:

答案 0 :(得分:2)

在编译/插入应用程序之前,您正在注入PeopleController模块。检查JS文件的顺序。载入 PeopleController.js 然后 app.js

答案 1 :(得分:1)

您需要从模块依赖项中移除peopleController ==&gt; angular.module('starter', ['ionic'])

它是一个控制器而不是一个模块。

修改

更改app.controller('PeopleController',...) 也是angular.module('starter').controller('PeopleController',...)

添加Angular文件和PeopleController.js文件

<!-- your app's js -->
<script src="angular/angular.min.js"></script>
<!-- ionic Dependencies ? -->
<script src="js/app.js"></script>
<script src="js/PeopleController.js"></script>

答案 2 :(得分:1)

问题最终是在 JS文件序列中,尝试在peopleController.js之后加载app.js文件,但什么都没有。

之后只需在加载peopleController.js之后立即加载 app.js,它就像魅力一样。

null

现在整个项目:

<强>的index.html

  <script src="js/app.js"></script>
  <script src="js/controllers/people_controller.js"></script> 

<强> app.js

 <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
        <title></title>

        <link href="lib/ionic/css/ionic.css" rel="stylesheet">
        <link href="css/style.css" rel="stylesheet">

        <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
        <link href="css/ionic.app.css" rel="stylesheet">
        -->

        <!-- ionic/angularjs js -->


        <script src="lib/ionic/js/ionic.bundle.js"></script>


        <!-- cordova script (this will be a 404 during development) -->
        <script src="cordova.js"></script>

        <!-- your app's js -->
        <script src="js/app.js"></script>
          <script src="js/controllers/people_controller.js"></script>
      </head>
       <body ng-app="starter">

        <ion-pane>
          <ion-header-bar class="bar-stable">
            <h1 class="title">Ionic Blank Starter</h1>
          </ion-header-bar>
          <ion-content>
          <ul class="list" ng-controller="PeopleController">
    <li class="item" ng-repeat="person in people">
    <h3>{{person.name}}</h3>

    </li>
    </ul>

          </ion-content>
        </ion-pane>

      </body>
    </html>

<强> peopleController.js:

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

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if(window.cordova && window.cordova.plugins.Keyboard) {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

      // Don't remove this line unless you know what you are doing. It stops the viewport
      // from snapping when text inputs are focused. Ionic handles this internally for
      // a much nicer keyboard experience.
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})