javascript - 重用 ionic 示例

标签 javascript angularjs ionic-framework

我正在尝试重用这个 ionic 示例 http://codepen.io/anon/pen/qbqqEb ,当我尝试在本地复制此示例代码时,我不断收到以下错误。我将 app.js 替换为 js,将 index.html 替换为 HTML 代码。

下面是app.js

angular.module('ionicApp', ['ionic'])

.config(function($stateProvider, $urlRouterProvider) {

  $stateProvider
    .state('eventmenu', {
      url: "/event",
      abstract: true,
      templateUrl: "templates/event-menu.html"
    })
    .state('eventmenu.home', {
      url: "/home",
      views: {
        'menuContent' :{
          templateUrl: "templates/home.html"
        }
      }
    })
    .state('eventmenu.checkin', {
      url: "/check-in",
      views: {
        'menuContent' :{
          templateUrl: "templates/check-in.html",
          controller: "CheckinCtrl"
        }
      }
    })
    .state('eventmenu.attendees', {
      url: "/attendees",
      views: {
        'menuContent' :{
          templateUrl: "templates/attendees.html",
          controller: "AttendeesCtrl"
        }
      }
    })

  $urlRouterProvider.otherwise("/event/home");
})

.controller('MainCtrl', function($scope, $ionicSideMenuDelegate) {
  $scope.attendees = [
    { firstname: 'Nicolas', lastname: 'Cage' },
    { firstname: 'Jean-Claude', lastname: 'Van Damme' },
    { firstname: 'Keanu', lastname: 'Reeves' },
    { firstname: 'Steven', lastname: 'Seagal' },
    { firstname: 'varun', lastname: 'bhavsar' },
  ];

  $scope.toggleLeft = function() {
    $ionicSideMenuDelegate.toggleLeft();
  };
})

.controller('CheckinCtrl', function($scope) {
  $scope.showForm = true;

  $scope.shirtSizes = [
    { text: 'Large', value: 'L' },
    { text: 'Medium', value: 'M' },
    { text: 'Small', value: 'S' }
  ];

  $scope.attendee = {};
  $scope.submit = function() {
    if(!$scope.attendee.firstname) {
      alert('Info required');
      return;
    }
    $scope.showForm = false;
    $scope.attendees.push($scope.attendee);
  };

})

.controller('AttendeesCtrl', function($scope) {

  $scope.activity = [];
  $scope.arrivedChange = function(attendee) {
    var msg = attendee.firstname + ' ' + attendee.lastname;
    msg += (!attendee.arrived ? ' has arrived, ' : ' just left, '); 
    msg += new Date();
    $scope.activity.push(msg);
    if($scope.activity.length > 3) {
      $scope.activity.splice(0, 1);
    }
  };

});

下面是html

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

    <title>Side Menus123</title>

    <link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet">
    <script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script>
        <script src="js/app.js"></script>

  </head>

  <body ng-controller="MainCtrl">

    <ion-nav-view></ion-nav-view>

    <script id="templates/event-menu.html" type="text/ng-template">
      <ion-side-menus enable-menu-with-back-views="false">

        <ion-side-menu-content>
          <ion-nav-bar class="bar-positive">
            <ion-nav-back-button>
            </ion-nav-back-button>

            <ion-nav-buttons side="left">
              <button class="button button-icon button-clear ion-navicon" menu-toggle="left">
              </button>
            </ion-nav-buttons>
          </ion-nav-bar>

          <ion-nav-view name="menuContent"></ion-nav-view>
        </ion-side-menu-content> 

        <ion-side-menu side="left">
          <ion-header-bar class="bar-assertive">
            <h1 class="title">Left Menu</h1>
          </ion-header-bar>
          <ion-content>
            <ul class="list">
              <!-- Note each link has the 'menu-close' attribute so the menu auto closes when clicking on one of these links -->
              <a href="#/event/check-in" class="item" menu-close>Check-in</a>
              <a href="#/event/attendees" class="item" menu-close>Attendees</a>
            </ul>
          </ion-content>
        </ion-side-menu>

      </ion-side-menus>
    </script>

    <script id="templates/home.html" type="text/ng-template">
      <ion-view view-title="Welcome">
        <ion-content class="padding">
          <p>Swipe to the right to reveal the left menu.</p>
          <p>(On desktop click and drag from left to right)</p>
        </ion-content>
      </ion-view>
    </script>

    <script id="templates/check-in.html" type="text/ng-template">
      <ion-view view-title="Event Check-in">
        <ion-content>
          <form class="list" ng-show="showForm">
            <div class="item item-divider">
              Attendee Info
            </div>
            <label class="item item-input">
              <input type="text" placeholder="First Name" ng-model="attendee.firstname">
            </label>
            <label class="item item-input">
              <input type="text" placeholder="Last Name" ng-model="attendee.lastname">
            </label>
            <div class="item item-divider">
              Shirt Size
            </div>
            <ion-radio ng-repeat="shirtSize in shirtSizes"
                       ng-value="shirtSize.value"
                       ng-model="attendee.shirtSize">
              {{ shirtSize.text }}
            </ion-radio>
            <div class="item item-divider">
              Lunch
            </div>
            <ion-toggle ng-model="attendee.vegetarian">
              Vegetarian
            </ion-toggle>
            <div class="padding">
              <button class="button button-block" ng-click="submit()">Checkin</button>
            </div>
          </form>

          <div ng-hide="showForm">
            <pre ng-bind="attendee | json"></pre> 
            <a href="#/event/attendees">View attendees</a>
          </div>
        </ion-content>
      </ion-view>
    </script>

    <script id="templates/attendees.html" type="text/ng-template">
      <ion-view view-title="Event Attendees">
        <ion-content>
          <div class="list">
            <ion-toggle ng-repeat="attendee in attendees | orderBy:'firstname' | orderBy:'lastname'"
                        ng-model="attendee.arrived"
                        ng-change="arrivedChange(attendee)">
              {{ attendee.firstname }}
              {{ attendee.lastname }}
            </ion-toggle>
            <div class="item item-divider">
              Activity
            </div>
            <div class="item" ng-repeat="msg in activity">
              {{ msg }}
            </div>
          </div>
        </ion-content>
      </ion-view>
    </script>

  </body>
</html>

Error: [ng:areq] Argument 'MainCtrl' is not a function, got undefined http://errors.angularjs.org/1.4.3/ng/areq?p0=MainCtrl&p1=not%20a%20function%2C%20got%20undefined at REGEX_STRING_REGEXP (ionic.bundle.js:13249) at assertArg (ionic.bundle.js:14951) at assertArgFn (ionic.bundle.js:14961) at ionic.bundle.js:22156 at setupControllers (ionic.bundle.js:21218) at nodeLinkFn (ionic.bundle.js:21258) at compositeLinkFn (ionic.bundle.js:20722) at compositeLinkFn (ionic.bundle.js:20726) at publicLinkFn (ionic.bundle.js:20597) at ionic.bundle.js:14816(anonymous function) @ ionic.bundle.js:25511ident.$get @ ionic.bundle.js:22290parent.$get.Scope.$apply @ ionic.bundle.js:29128bootstrapApply @ ionic.bundle.js:14814invoke @ ionic.bundle.js:17631doBootstrap @ ionic.bundle.js:14812bootstrap @ ionic.bundle.js:14832angularInit @ ionic.bundle.js:14726(anonymous function) @ ionic.bundle.js:41540trigger @ ionic.bundle.js:16177eventHandler @ ionic.bundle.js:16452

最佳答案

必须定义 ng-app 指令中定义的起始模块。

<html ng-app="">

应该是

<html ng-app="ionicApp">

关于javascript - 重用 ionic 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34515775/

相关文章:

javascript - 使用 AngularJS 1.4 像 Replaceable Tiles 一样开发 Windows?

html - BUG - IOS 中选择标签没有完成按钮

angular - ionic 4分页

javascript - 通过 koa 提供静态文件并使用基本身份验证

javascript - 使用数组解构从 Angular $injector 读取多个值

javascript - AngularJs ngClick 缓存更新问题

javascript - setTimeout 在异步代码中不起作用

javascript - 如何解决 ionic 框架中的类型错误

javascript - 如何使用 Processing.js 水平翻转图像?

c# - 主视图模型中的绑定(bind)