【问题标题】:Debugging angular ui-router under ionic在 ionic 下调试 Angular ui-router
【发布时间】:2016-01-05 00:16:57
【问题描述】:

我对 Angularjs 和 Ionic 还很陌生,我正试图绕开基于状态的路由。最大的障碍是,如果没有合适的方式来调试正在发生的事情,似乎很难深入研究。

有一些 help for debugging angularjs ui-routing in this question and answer - 但这个例子只适用于 AngularJS 而不是 Ionic,我正在努力弄清楚如何在 Ionic 中实现这个解决方案。

在 AngularJS 中,调试代码会放在这里:

angular.module('MyModule').run(['$rootScope',function($rootScope){ // put the event handlers here }]);

但在 Ionic 中,相应的代码如下所示:

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 && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
  });  
})

谁能帮我理解如何在这里注入调试代码?

【问题讨论】:

  • 你仍然可以做同样的事情; Ionic 只是在幕后使用 Angular。您还可以在启动时拥有多个“运行”功能。
  • 谢谢,“多次运行”是真正有帮助的部分。我希望你能把这个作为答案发布,这样我就可以投票并检查它!

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


【解决方案1】:

感谢George Stocker 的评论,我明白了。生成的代码如下所示:

angular.module('starter', [])
.run(['$rootScope',function($rootScope){ 

    $rootScope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){
          console.log('$stateChangeStart to '+toState.to+'- fired when the transition begins. toState,toParams : \n',toState, toParams);
        });
    $rootScope.$on('$stateChangeError',function(event, toState, toParams, fromState, fromParams, error){
          console.log('$stateChangeError - fired when an error occurs during transition.');
          console.log(arguments);
        });
    $rootScope.$on('$stateChangeSuccess',function(event, toState, toParams, fromState, fromParams){
          console.log('$stateChangeSuccess to '+toState.name+'- fired once the state transition is complete.');
        });
    $rootScope.$on('$viewContentLoaded',function(event){
          console.log('$viewContentLoaded - fired after dom rendered',event);
        });
    $rootScope.$on('$stateNotFound',function(event, unfoundState, fromState, fromParams){
          console.log('$stateNotFound '+unfoundState.to+'  - fired when a state cannot be found by its name.');
          console.log(unfoundState, fromState, fromParams);
        });
}])

【讨论】:

    【解决方案2】:

    在这里,您可以将AngularJS logging service 注入您的Ionic 项目,只需一个run 方法调用:

    angular.module('starter', ['ionic', 'starter.controllers', 'starter.services'])
        .run(['$rootScope', '$log', '$ionicPlatform', function ($rootScope, $log, $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 && window.cordova.plugins.Keyboard) {
                    cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
                    cordova.plugins.Keyboard.disableScroll(true);
                }
                if (window.StatusBar) {
                    // org.apache.cordova.statusbar required
                    StatusBar.styleDefault();
                }
            });
    
            // Debug stuff...
            $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
                $log.debug('$stateChangeStart to ' + toState.name + '- fired when the transition begins. toState,toParams : \n', toState, toParams);
            });
            $rootScope.$on('$stateChangeError', function (event, toState, toParams, fromState, fromParams, error) {
                $log.debug('$stateChangeError - fired when an error occurs during transition.');
                $log.debug(arguments);
            });
            $rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
                $log.debug('$stateChangeSuccess to ' + toState.name + '- fired once the state transition is complete.');
            });
            // $rootScope.$on('$viewContentLoading',function(event, viewConfig){
            //   // runs on individual scopes, so putting it in "run" doesn't work.
            //   $log.debug('$viewContentLoading - view begins loading - dom not rendered',viewConfig);
            // });
            $rootScope.$on('$viewContentLoaded', function (event) {
                $log.debug('$viewContentLoaded - fired after dom rendered', event);
            });
            $rootScope.$on('$stateNotFound', function (event, unfoundState, fromState, fromParams) {
                $log.debug('$stateNotFound ' + unfoundState.name + '  - fired when a state cannot be found by its name.');
                $log.debug(unfoundState, fromState, fromParams);
            });
        }])
    

    对于生产,您可以在配置块中禁用调试:

    .config(function($logProvider){
        $logProvider.debugEnabled(false);
    });
    

    【讨论】:

    • 请注意,toState.to 未定义。应该是toState.name
    • 感谢@MariaInesParnisari -- 我已经编辑了这个答案
    猜你喜欢
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    • 2014-09-16
    相关资源
    最近更新 更多