为什么我的离子状态提供者不起作用?

时间:2017-01-19 20:09:31

标签: javascript angularjs ionic-framework

所以我想扩展我的项目以更好地使用移动设备,并从使用Angularjs改为Ionic / Angular。我让控制器工作正常,但由于某种原因,我的$ stateProvider根本没有做出反应。 / p>

这是我的主要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(' / login')时会出现错误,错误:无法解析' /登录'来自州'

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>

1 个答案:

答案 0 :(得分: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启动器并查看nav-view

ionic start myApp sidemenu

例如