Search code examples
javascriptangularjscordovaionic-frameworkangularjs-scope

$scope inside $ionicplatform doesn't work


I have been developing an application, but this doesn't seem to work. normally if we add a value to the scope I think it needs to get updated in the application.. here is the index.html

<body ng-app="starter">
<ion-nav-view>
  <ion-view>
  <ion-side-menus>
        <ion-side-menu-content>
          <ion-nav-bar class="top-nav">

          </ion-nav-bar>
          <ion-content class="body" ng-controller="frontpage">
          ss {{njk}} dd
          </ion-content>
        </ion-side-menu-content>
        <ion-side-menu side="left">

        </ion-side-menu>
  </ion-side-menus>
  </ion-view>
</ion-nav-view>

and here is the app.js

var main=angular.module('starter', ['ionic','ngCordova'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
  cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
  cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
  StatusBar.styleDefault();
}
  });

});

main.controller("frontpage", function($scope,$cordovaFile,$ionicPlatform) {
  $ionicPlatform.ready(function() {
  $scope.njk='sss';
  alert($scope.njk);
 });
});

the problem is with the $scope.njk . I have added it inside the $ionicPlatform.ready and have assigned the value, but it doesn't reflect in the app. but the alert box is coming with the value. I need it inside the ready function badly. cause cordova.file won't work outside that. I have tried the $rootScope too. But it doesn't seem to work.


Solution

  • Try it using:

    $ionicPlatform.ready(function() {
      $scope.$apply(function () {
        $scope.njk='sss';
      });
    });
    

    Angular usually handles digesting automatically but however if you change any model outside of the Angular context (in this case the $ionicPlatform.ready function), then you need to inform Angular of the changes by calling $apply() manually. It’s like telling Angular that you are changing some models and it should fire the watchers so that your changes propagate properly.