无法使用 angularjs 和离子将数据从控制器拉到视图

Unable to pull data from controller to a view using angularjs and ionic


我正在尝试使用 Angularjs 和离子框架为 Android 开发应用程序。 该应用程序在桌面浏览器上运行良好,但在英特尔 XDK 模拟器或 Android 设备上 运行 时无法从控制器获取数据。

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">
    <link href="css/custom.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>

    <script src="js/angular-resource.min.js"></script>
    <script src="js/angular-route.min.js"></script>
    <!-- your app's js -->
    <script src="js/app.js"></script>
    <script src="js/controller.js"></script>
  </head>
  <body ng-app="engageApp">

    <div ng-view></div>
  </ion-pane>
  </body>
</html>

app.js

    'use strict';
angular.module('engageApp', ['ionic','ngRoute','ngResource','engageAppModule'])

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

.config(['$routeProvider', function($routeProvider){
  $routeProvider.
    when('/onboardcategory',{
      templateUrl: 'partials/onboard/category.html',
      controller: 'OnboardCtrl'
    }).
   .
   .
   .
    otherwise({
      redirectTo: '/home'
    });
}]);

controller.js

'use strict';

var engageAppControllers = angular.module('engageAppModule',[]);

engageAppControllers.controller('OnboardCtrl',['$scope', '$filter', '$location', function($scope , $filter, $location){

    $scope.mytempvar = "Hello World!";

    $scope.categories =
     [
       {
         id: "mc1",
         name: "Dance and Music"
       },
       {
         id: "mc2",
         name: "Outdoors"
       },
       {
         id: "mc3",
         name: "Fitness and Yoga"
       },
       {
         id: "mc4",
         name: "Others",
       }
     ];
   $scope.go = function ( path ) {
      $location.path( path );
   };
    ...

category.html(查看)

  <ion-pane>
  <ion-content>
    <div class="shrunk-header">
      <div class="logo-container">
        <img src="img/engagelogo.png" alt="logo">
      </div>
      <div class="header-info">
        <h3>Select Category</h3>
         ...
      </div>
      <div ng-controller="OnboardCtrl" class="content-container">
        <p>{{mytempvar}}</p>
        <ul class="list">
          <li class="item item-checkbox" data-ng-repeat="category in categories"> 
            {{category.name}}
          </li>
        </ul>
      </div>
      ...
      </div>
    </div>
  </ion-content>
</ion-pane>

模拟器显示如下结果: Intel XDK screenshot

知道我做错了什么吗??

谢谢

您好尝试使用此解决方案对我有用
第 1 步:在您的 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="js/ng-cordova.min.js"></script>
    <script src="cordova.js"></script>
    <script src="lib/ngSticky/dist/sticky.min.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
  </head>
  <body ng-app="starter">
    <ion-nav-view></ion-nav-view>
  </body>
</html>

第 2 步:在 app.js 文件中为 <ion-nav-view></ion-nav-view> 提供路由

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

  .state('page1',{
    url:"/page1",
    templateUrl:"templates/page1.html",
    controller: 'ExampleCtrl'
  })  
  $urlRouterProvider.otherwise("/page1");  
})

Step3: 创建一个文件夹templates 并将其添加到www 下,文件夹结构将是www/templates/page1.html

<ion-view view-title="Title">
  <ion-content>
  <div class="shrunk-header">
      <div class="logo-container">
        <img src="img/engagelogo.png" alt="logo">
      </div>
      <div class="header-info">
        <h3>Select Category</h3>
      </div>
      <div ng-controller="ExampleCtrl" class="content-container">
        <p>{{mytempvar}}</p>
        <ul class="list">
          <li class="item item-checkbox" data-ng-repeat="category in categories"> 
            {{category.name}}
          </li>
        </ul>
      </div>
      </div>
    </div>
  </ion-content>
</ion-view>

第 4 步:在上面的 html 页面中,我创建了一个控制器作为 ExampleCtrl 你可以自己重命名它并将你的控制器代码编写为
注意:我在 app.js 文件中仅将我的控制器代码写为

.controller('ExampleCtrl', ['$scope','$ionicNavBarDelegate','$ionicPlatform','$state', function ($scope,$ionicNavBarDelegate,$ionicPlatform,$state) {
 $scope.mytempvar = "Hello World!";

    $scope.categories =
     [
       {
         id: "mc1",
         name: "Dance and Music"
       },
       {
         id: "mc2",
         name: "Outdoors"
       },
       {
         id: "mc3",
         name: "Fitness and Yoga"
       },
       {
         id: "mc4",
         name: "Others",
       }
     ];

}])

有任何疑问,请回复