2017-01-19 20 views
0

所以我想扩展我的项目,以更好地使用移动设备,并从使用Angularjs改为Ionic/Angular.I控制器工作正常,但由于某种原因,我的$ stateProvider根本没有反应。为什么我的离子状态提供程序不工作?

这是我的主要app.js

angular.module('mainApp', ['ionic']) 

.run(function($ionicPlatform) { 
    $ionicPlatform.ready(function() { 
    if(window.cordova && window.cordova.plugins.Keyboard) { 
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard 
    // for form inputs) 
     cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); 

    // Don't remove this line unless you know what you are doing. It stops the viewport 
    // from snapping when text inputs are focused. Ionic handles this internally for 
    // a much nicer keyboard experience. 
     cordova.plugins.Keyboard.disableScroll(true); 
    } 
    if(window.StatusBar) { 
     StatusBar.styleDefault(); 
    } 
    }); 
}) 

这里是我的config.js,这似乎并没有在所有的工作,尽管它并不在控制台发出任何错误。如果它无法找到模板,它不应该给出错误吗?或者如果它不能解决状态?

angular.module('mainApp', ['ionic']) 

.config(function($stateProvider, $urlRouterProvider) { 
    $stateProvider. 
     state('/frontpage', { 
     templateUrl: './views/frontpage.html', 
     controller: 'frontpageController', 
     activetab: 'frontpage' 
    }). 
     state('/login', { 
     templateUrl: './views/login.html', 
     controller: 'loginController', 
     activetab: 'login' 
     }). 
     state('/register', { 
     templateUrl: './views/register.html', 
     controller: 'registerController', 
     activetab: 'register' 
     }). 
     otherwise({ 
     redirectTo: '/frontpage' 
     }); 
}) 

这是我controllers.js我只测试了我的作品的TestController,虽然当$ state.go(“/登录”)被称为它给出了一个错误,错误:无法从解决“/登录”状态'

angular.module('mainApp', ['ionic']) 

.controller('mainController', function($scope, $state){ 
    $scope.$state=$state; 
}) 

.controller('frontpageController', function($scope){ 
    $scope.test="frontpage"; 
}) 

.controller('testController', function($scope, $state){ 
    $scope.test="hmm"; 
    $scope.testi = function(){ 
      $state.go('/login'); 
      $scope.test="lol"; 
      console.log('test'); 
    }; 
}) 

.controller('loginController', function($scope, $http, $state){ 
    $scope.rememberMe = false; 
    $scope.authenticate = function(user){ 
     $scope.loginError = ""; 
     $http.post('http://localhost:8080/api/authenticate', user) 
     .success(function(response) { 
      if (response.success === true){ 
       $state.go("/frontpage"); 
       console.log(response.msg); 
      }else if (response.success === false){ 
       console.log(response.msg); 
       $scope.loginError = "login failed"; 
      }; 
     }) 
     .error(function(response, status) {            
      alert('Error! ' + status + ' : ' + response);        
     }); 
    }; 
}) 

.controller('registerController', function($scope, $http, $location){ 
    $scope.register = function(user){ 
     $scope.registerError = ""; 
     if (user.password === user.repassword) 
      $http.post('http://localhost:8080/api/users', user) 
       .success(function(response) { 
        console.log(user); 
       }) 
       .error(function(response, status) {            
        alert('Error! ' + status + ' : ' + response);        
       }); 
     else 
      $scope.registerError = "passwords do not match";   
    } 
}); 

以防万一,这里是我的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>saunaguard</title> 

    <link rel="manifest" href="manifest.json"> 

    <!-- un-comment this code to enable service worker 
    <script> 
     if ('serviceWorker' in navigator) { 
     navigator.serviceWorker.register('service-worker.js') 
      .then(() => console.log('service worker installed')) 
      .catch(err => console.log('Error', err)); 
     } 
    </script>--> 

    <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) --> 


    <!-- your app's js --> 

    <script src="js/app.js"></script> 
    <script src="js/config.js"></script> 
    <script src="js/controllers.js"></script> 

    </head> 
    <body ng-app="mainApp"> 
    <div ng-controller = "testController"> 
     <ul id ="selection"> 
      <li ng-class="{active: $state.current.activetab == 'frontpage'}"><a href ="#frontpage">Home</a></li> 
      <li ng-class="{active: $state.current.activetab == 'login'}"><a href ="#login">Login</a></li> 
      <li ng-class="{active: $state.current.activetab == 'register'}"><a href ="#register">Register</a></li> 
     </ul> 
     <button ng-click="testi()">Submit</button> {{test}} 
    </div> 
    <ion-nav-view> 
     <ion-pane> 
      <ion-content> 

      </ion-content> 
     </ion-pane> 
    </ion-nav-view> 
    </body> 
</html> 

回答

0

正如我所看到的,你没有你的状态添加到具体的导航视图

例如,这是它如何工作的侧菜单

<ion-nav-view name="menuContent"></ion-nav-view> 

然后index.html中你必须

<body ng-app="starter"> 
    <ion-nav-view></ion-nav-view> 
</body> 

然后你基地的app.js查看此“menuContent”导航视图

.state('app.playlists', { 
    url: '/playlists', 
    views: { 
     'menuContent': { 
      templateUrl: 'templates/playlists.html', 
      controller: 'PlaylistsCtrl' 
     } 
    } 
}) 

我没有想粘贴整个结构,这将帮助你弄清楚,或者只是得到sidemenu启动,并期待在导航视图

ionic start myApp sidemenu 

例如