【问题标题】:ionic : $location.path works only if I use $scope.$apply()ionic : $location.path 仅在我使用 $scope.$apply() 时才有效
【发布时间】:2016-09-10 21:44:40
【问题描述】:

我对 ionic 非常陌生,并正在尝试实现相机功能。我在 routes.js 中有如下设置状态

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

  // Ionic uses AngularUI Router which uses the concept of states
  // Learn more here: https://github.com/angular-ui/ui-router
  // Set up the various states which the app can be in.
  // Each state's controller can be found in controllers.js
  $stateProvider
  .state('capture', {
    url: '/',
    templateUrl: 'templates/capture.html',
    controller: 'captureCtrl'
  })

  .state('selectColor', {
    url: '/selectColor',
    templateUrl: 'templates/selectColor.html',
    controller: 'selectColorCtrl'
  })

$urlRouterProvider.otherwise('/')
})

在 capture.html 中,我有一个图像可以点击使用下面的方法打开哪个相机

$scope.capturePhoto = function() {
    // Take picture using device camera and retrieve image as base64-encoded string
    navigator.camera.getPicture($scope.onPhotoDataSuccess, $scope.onFail, {
      quality: 30,
      targetWidth: 600,
      targetHeight: 600,
      destinationType: navigator.camera.PictureSourceType.FILE_URI,
      saveToPhotoAlbum: true
    });
  }

  $scope.onPhotoDataSuccess = function(imageURI) {
    console.log(imageURI);
    $location.path('/selectColor');
  }

  $scope.onFail = function(message) {
    //alert('Failed because: ' + message);
  }

在照片成功时,控制权进入 $scope.onPhotoDataSuccess 方法,我试图使用 $location.path('selectColor') 移动到 /selectColor 但它没有正常呈现 selectColor.html 页面,我必须使用 $scope.$apply() 使其出现。

如果你已经在 Angular 中,我有 Angular 和 AFAIK 的经验,那么你不需要调用 $scope.$apply()

在我的情况下出了什么问题,是否有不同的方法可以在 ionic 而不是 $location.path() 中路由到另一个页面??

谢谢, 普拉提克

【问题讨论】:

  • 你为什么不使用$state.go
  • 我比较熟悉 angular。 Angular-ui 对我来说也是新的。让我试一试。

标签: angularjs ionic-framework angular-ui-router angular-ui


【解决方案1】:

这完全取决于从哪里调用 $scope.onPhotoDataSuccess,当您考虑您是否“在角度内”时。鉴于它可能是从 navigator.camera 调用的,它不会出现在角度功能(ng-click、ng-submit 等)的某个位置,您必须继续调用 $scope.$apply() 才能获得它才能正常工作。没有错,这就是它应该的工作方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    • 2018-01-05
    • 2022-11-16
    • 2016-05-19
    • 2019-12-19
    相关资源
    最近更新 更多