【问题标题】:Argument 'PeopleController' is not a function, got undefined IonicJS参数 'PeopleController' 不是一个函数,得到了未定义的 IonicJS
【发布时间】:2016-03-29 22:49:35
【问题描述】:
  1. 使用 IonicJS 并渲染一些 json 细节。
  2. 出现错误:“参数 'PeopleController' 不是函数, 未定义” 我检查了文件 people_controller.js

(函数() { 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>

我做错了什么?

【问题讨论】:

    标签: javascript angularjs ionic-framework


    【解决方案1】:

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

    【讨论】:

    • 我不知道发生了什么,尝试了你的方法,但没有任何效果。之后我尝试在 app.js 之后加载 peoplecontroller.js 并且它起作用了。
    【解决方案2】:

    您需要从模块依赖项中删除 peopleController ==> 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>
    

    【讨论】:

    • 忘记检查 html... 已编辑
    【解决方案3】:

    问题最终出在JS文件的序列上,尝试在peopleController.js之后加载app.js文件,但一无所获。

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

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

    现在整个项目:

    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>
              <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>
    

    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();
        }
      });
    })
    

    peopleController.js:

    (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.'); 
    }); 
    } 
    );
    })(); 
    

    【讨论】:

      猜你喜欢
      • 2018-01-14
      • 1970-01-01
      • 2013-03-22
      • 1970-01-01
      • 2015-09-10
      • 2016-10-28
      • 2016-12-17
      • 2016-05-22
      • 2014-06-29
      相关资源
      最近更新 更多