将现有的Angular应用程序转换为Ionic。

3
我遇到了一个问题,我想要在ionic中迁移一个angular应用程序。我在angular中使用了ui-router,这是我的app.js文件(来自这里:https://github.com/tarlepp/angular-sailsjs-boilerplate-frontend/blob/master/src/app/app.js)。对于我的项目,我没有修改过此文件。现在我想要重复使用这个文件,所以我用ionic提供的app.js替换了它,并添加了一些内容。
  $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();
}});

在app.run内部

这是我的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="cordova.js"></script>

<!-- your app's js -->
<script src="js/app.js"></script>
</head>
<body ng-app="frontend">
<ion-pane>
  <ion-header-bar class="bar-stable">
    <h1 class="title">Ionic Blank Starter</h1>
  </ion-header-bar>
  <ion-content>
  </ion-content>
</ion-pane>
</body>
</html>

我只是用frontend替换了ng-app的名称,结果ionic主题无法正常加载。

我不知道问题是来自ui-retour依赖还是其他什么原因?我觉得我漏掉了某些东西。

注意:我知道我将不得不更改项目中的html和css文件,但对于index.html,我只有ionic提供的内容,并且我没有带有'ionic blank starter'的ionic选项卡,就像我开始一个空白的ionic项目一样。

这是我的路由器改变:

enter code here
(function() {
'use strict';



 angular.module('frontend', [
'ionic',
'frontend-templates',
'frontend.core',
'frontend.myportfolio',
'frontend.mysocial',
'frontend.myleads',
'frontend.admin'
  ]);

 angular.module('frontend')
 .config([
  '$stateProvider', '$locationProvider', '$urlRouterProvider',      '$httpProvider', '$sailsSocketProvider',
  '$tooltipProvider', 'cfpLoadingBarProvider',
  'toastrConfig',
  'AccessLevels',
  function config(
    $stateProvider, $locationProvider, $urlRouterProvider,   $httpProvider, $sailsSocketProvider,
    $tooltipProvider, cfpLoadingBarProvider,
    toastrConfig,
    AccessLevels
  ) {



    $httpProvider.defaults.useXDomain = true;

    delete $httpProvider.defaults.headers.common['X-Requested-With'];

    // Add interceptors for $httpProvider and $sailsSocketProvider
    $httpProvider.interceptors.push('AuthInterceptor');
    $httpProvider.interceptors.push('ErrorInterceptor');

    // Iterate $httpProvider interceptors and add those to $sailsSocketProvider
    angular.forEach($httpProvider.interceptors, function iterator(interceptor) {
      $sailsSocketProvider.interceptors.push(interceptor);
    });

    // Set tooltip options
    $tooltipProvider.options({
      appendToBody: true
    });

    // Disable spinner from cfpLoadingBar
    cfpLoadingBarProvider.includeSpinner = false;
    cfpLoadingBarProvider.latencyThreshold = 200;

    // Extend default toastr configuration with application specified configuration
    angular.extend(
      toastrConfig,
      {
        allowHtml: true,
        closeButton: true,
        extendedTimeOut: 3000
      }
    );

    // Yeah we wanna to use HTML5 urls!
    $locationProvider
      .html5Mode({
        enabled: true,
        requireBase: false
      })
      .hashPrefix('!')
    ;

    // Routes that needs authenticated user
    $stateProvider
      .state('profile', {
        abstract: true,
        template: '<ui-view/>',
        data: {
          access: AccessLevels.user
        }
      })



    $urlRouterProvider.otherwise('/myportfolio/mydatabase');
    }
   ])
   ;


 angular.module('frontend')
   .run([
     '$rootScope', '$state', '$injector','$ionicPlatform',
     'editableOptions',
  'AuthService',
  function run(
    $rootScope, $state, $injector,$ionicPlatform,
    editableOptions,
    AuthService
  ) {

    $rootScope.$on('$stateChangeStart', function   stateChangeStart(event, toState) {
      if (toState.url=='/login' || toState.url=='/register' ) {
        $rootScope.myStyle = {'background-color':'#333'}
        $rootScope.headerIsloaded = false;
        $rootScope.sidebarIsloaded = false;

      }else{

        $rootScope.myStyle = {};
        $rootScope.headerIsloaded = true;
        $rootScope.sidebarIsloaded = true

      };

      if (!AuthService.authorize(toState.data.access)) {
        event.preventDefault();
        console.log(toState.data.access)

        $state.go('auth.login');
      }
    });

    // Check for state change errors.
    $rootScope.$on('$stateChangeError', function stateChangeError(event, toState, toParams, fromState, fromParams, error) {
      event.preventDefault();

      $injector.get('MessageService')
        .error('Error loading the page');

      $state.get('error').error = {
        event: event,
        toState: toState,
        toParams: toParams,
        fromState: fromState,
        fromParams: fromParams,
        error: error
      };

      return $state.go('error');
    });


    //bout de code ionic

    $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();
      }
    });

  }
])
 ;
  }());

请提供您的路由器。 - Anas Omar
我编辑了我的问题,添加了我的路由器。 - moubert
即使我在“前端”依赖项中仅使用Ionic,也无法解决我的问题。 - moubert
你需要更具体一些。你遇到了什么类型的错误?只是视觉上的问题还是控制台中有错误信息?你应该先检查一下这个。 - devconcept
它只是可视化的:就像我说的,问题似乎是我的路由器不工作,因为Ionic的启动项目的主页没有正确显示。看看我的笔记:“注意:我知道我将不得不更改项目的HTML和CSS文件,但对于index.html,我只有Ionic提供的内容,我没有带有'ionic blank starter'的Ionic选项卡,就好像我开始一个空白的Ionic项目一样。” - moubert
1个回答

0
你不是忘了为ui.router添加标记以将状态视图注入进去吗?
<ion-view view-title="your-view">
</ion-view>

如果你使用以下代码创建一个新的应用:

ionic start myApp

它的默认模板是tab,其中包含以下内容在它的index.html中。

  <body ng-app="starter">
    <!--
      The nav bar that will be updated as we navigate between views.
    -->
    <ion-nav-bar class="bar-stable">
      <ion-nav-back-button>
      </ion-nav-back-button>
    </ion-nav-bar>
    <!--
      The views will be rendered in the <ion-nav-view> directive below
      Templates are in the /templates folder (but you could also
      have templates inline in this html file if you'd like).
    -->
    <ion-nav-view></ion-nav-view>
  </body>

这里的ion-nav-view是允许ui.router插入其视图的组件


我正在处理新项目的首页,因此不。 - moubert
如果您查看index.html上的标记,就会发现没有视图供ui.router填充。指令ion-content不是ui.router的视图。 - vilsbole

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接