angularjs - 在 ionic 中转换现有的 Angular 应用程序

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

我想在 ionic 中迁移的 Angular 应用程序遇到问题

我将 ui-router 用于 angular,这是我的 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();
      }
    });

  }
])
 ;
  }());

最佳答案

你是不是错过了 ui.router 的标记来注入(inject)你的状态 View ?

<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 插入其 View

关于angularjs - 在 ionic 中转换现有的 Angular 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32480061/

相关文章:

javascript - Angular - 每个显示和隐藏元素的分隔变量

javascript - Angular JS UI-Router 如何在不重新加载页面的情况下更改导航栏中的 url

javascript - 阿拉伯语的 Ionic 日期时间选择器数字

css - 为什么标题比图像中显示的高度尺寸大?

javascript - Angularjs 中的幻灯片无法按预期工作

angularjs - 将相互依赖但不同的数据格式绑定(bind)到 Angular 中的输入

javascript - 使用自定义事件更新 Angular 模型

javascript - 幻灯片多重 promise 的替代方案

angularjs - Angular UI-Router 重定向登录用户远离登录路由

javascript - 内容安全策略不适用于 Ionic 服务