Search code examples
androidangularjsionic-frameworkintel-xdk

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



I am trying to develop an app for Android using Angularjs and ionic frameworks. The app works absolutely fine on a desktop browser but fails to fetch data from the controller when running on Intel XDK emulator or an Android device.

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 (view)

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

The emulator shows the following as result: Intel XDK screenshot

Any idea as to what I'm doing wrong??

Thanks


Solution

  • Hi Try with this solutions It worked for me
    Step1: In your Index.html page add this code

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

    Step 2: provide routings for <ion-nav-view></ion-nav-view> in app.js file as

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

    Step3: Create a folder templates and add it under www and the folder structure will be 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>
    

    Step4: In above html page I created a controller as ExampleCtrl you can rename It on your own and write your controller code as
    Note: I wrote my controller code in app.js file only as

    .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",
           }
         ];
    
    }])
    

    Having any queries, reply back