【问题标题】:AngularJS expressions stop working when adding controller to body将控制器添加到正文时,AngularJS 表达式停止工作
【发布时间】:2016-02-21 05:16:31
【问题描述】:

我尝试使用ionic framework 构建一个简单的待办事项列表应用程序,但是遇到了问题。
每当我将 ng-controllerng-reapeat 指令添加到我的代码中时,AngularJS 表达式就会停止工作。
这是我的代码:
index.html

<!DOCTYPE html>
<html ng-app="starter">
  <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>

    <ion-pane>
      <ion-header-bar class="bar-stable">
        <h1 class="title">ToDo</h1>
      </ion-header-bar>
      <ion-content>
        <ion-list>
          <ion-item ng-repeat="todo in TodoList">{{todo.name}}</ion-item>
        </ion-list>
      </ion-content>
    </ion-pane>
  </body>
</html>

app.js

var app = angular.module('ionicApp', ['ionic'])
app.controller('TodoCtrl', function($scope){
  $scope.TodoList = [
    {name: "Item 1"},
    {name: "Item 2"}
  ]
})

【问题讨论】:

    标签: javascript angularjs ionic-framework


    【解决方案1】:

    您在app.js 中将应用的核心模块定义为:

    var app = angular.module('ionicApp', ['ionic'])
    

    但是,在您的 html 中,您将应用定义为:

    <html ng-app="starter">
    

    在您的 ng-app 属性中,您将此应用程序定义为 Angular 应用程序,并且您正在引用一个不存在的模块 (starter)。您需要指定要使用的 Angular 模块,此时在您的 app.js 中是 ionicApp。因此,只要名称匹配,请尝试将 starter 重命名为 ionicApp,或者反过来。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多