2016-07-10 24 views
0

我是新的離子 我試圖做一個登錄應用兩個視圖之間切換簡單:AngularJS改變位置不工作

的index.html

<ion-pane> 
    <ion-header-bar class="bar-stable"> 
    <h1 class="title">Ionic Login App</h1> 
    </ion-header-bar> 
    <ion-content> 

      <div class="list list-inset"> 
       <label class="item item-input"> 
        <input type="text" placeholder="Email" ng-model="data.email"> 
       </label> 
       <label class="item item-input"> 
        <input type="password" placeholder="Password" ng-model="data.password"> 
       </label> 
      </div> 
      <button class="button button-block button-calm" ng-click="postLogin()">Login</button> 

    </ion-content> 
</ion-pane> 

logincontroller.js

app.controller( 'loginCtrl',函數($範圍,$ HTTP,$位置) {

$scope.data = {}; 
$scope.postLogin = function() 
{ 

    var data = 
    { 
      email: $scope.data.email, 
      password: $scope.data.password 
    }; 

    console.log('bonjour'); 
    $http.post("http://localhost/authproject/public/api/auth/login", data) 
    .then(
     function(response){ 
     // success callback 
     console.log('success'); 
     $location.path('/map'); 


     }, 
     function(response){ 
     // failure callback 

     console.log('error'); 
    $location.path('/index'); 

     } 
    ); 



} 

當我點擊一個按鈕登錄的URL變化,但頁面沒有改變

有人能告訴我如何解決這個問題嗎?感謝提前:)

+0

要再次同一個瀏覽頁面登錄後? –

+0

是的。我想導航map.html頁面 – Sarah

回答

1

在你app.js添加狀態爲map像這樣(用你自己的templateUrlcontroller值)。請務必在config()中添加$stateProvider。另外,包括ui.router到您的依賴項。事情是這樣的:

angular.module('starter', ['ionic', 'starter.controllers', 'starter.directives', 'ui.router']) 
    .config(function($stateProvider, $urlRouterProvider, $httpProvider, $locationProvider){ 

    $stateProvider 
     .state('map', { 
     url: '/map', 
     templateUrl: 'app/views/map.html', 
     controller: 'MapController' 
    }); 
... 

而在你index.html

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.18/angular-ui-router.min.js"></script>

+0

認爲你的迴應,我得到這個錯誤:ionic.bundle.js:16989錯誤:[$注射器:unpr]未知的提供者:$ stateProvider < - $狀態 http://errors.angularjs.org/1.2。 12/$噴油器/ unpr?p0 =%24stateProvider%20%3C-%20%24state – Sarah

+0

啊好的這個錯誤解決了:)當我點擊一個按鈕登錄url更改:http:// localhost:8100 /#/ map但頁面更改了#導致這個問題? – Sarah

+0

如果我解決了這個問題,請將我的答案標記爲解決方案。關於另一個問題,該頁面正在改變,這可能是因爲很多事情。你是否已將'templateUrl'改爲你的'html'文件? –

0

試試這個

app.controller('loginCtrl', function ($scope,$http,$location,$state) { 

$scope.data = {}; 
$scope.postLogin = function() 
{ 

    var data = 
    { 
      email: $scope.data.email, 
      password: $scope.data.password 
    }; 

    console.log('bonjour'); 
    $http.post("http://localhost/authproject/public/api/auth/login", data) 
    .then(
     function(response){ 
     // success callback 
     console.log('success'); 
     $location.path('/map'); 


     }, 
     function(response){ 
     // failure callback 

     console.log('error'); 
     $state.go('main'); 

     }); 
}) 

app.js

.config(function($stateProvider,$urlRouterProvider){ 
     .state('main', { 
     url:'/main', 
     templateUrl:'templates/main.html', 
     controller:'yourCtrl' 
    }) 

$urlRouterProvider.otherwise('/index'); 
}) 

添加的狀態現在使模板文件夾製作一個網頁名爲main.html

+0

我這樣做,我得到這個錯誤:app.js:11 Uncaught TypeError:angular.state不是一個函數(匿名函數)@ app.js:11 離子。 bundle.js:16989錯誤:[$ injector:unpr]未知提供者:$ stateProvider < - $ state – Sarah

+0

這是我的文件app.js:angular.module('starter',['ionic','starter.controllers', 'starter.directives']) .RUN(函數($ ionicPlatform){$ ionicPlatform.ready(函數(){ 如果(window.StatusBar){ StatusBar.styleDefault();} }); }) .state('main',{ url:'/ main', templateUrl:'templates/main.html', controller:'loginCtrl' }); – Sarah

+0

認爲:)在app.js中沒有錯誤,但是當我添加到登錄控制器$狀態我得到此錯誤:ionic.bundle.js:16989錯誤:[$ injector:unpr]未知提供者:$ stateProvider < - $狀態 – Sarah