2016-10-24 13 views
0

こんにちはevryoneは角度に問題があります。私はSPAを持っています:角度コードは以下の通りです:角度ngResourceはコントローラでは機能しません

angular.module('myApp', ['ngResource']).controller('loginController', 
    ['$scope', '$location', 'AuthService', 
    function ($scope, $location, AuthService) { 

    $scope.login = function() { 

     // initial values 
     $scope.error = false; 
     $scope.disabled = true; 

     // call login from service 
     AuthService.login($scope.loginForm.username, $scope.loginForm.password) 
     // handle success 
     .then(function() { 
      $location.path('/'); 
      $scope.disabled = false; 
      $scope.loginForm = {}; 
     }) 
     // handle error 
     .catch(function() { 
      $scope.error = true; 
      $scope.errorMessage = "Invalid username and/or password"; 
      $scope.disabled = false; 
      $scope.loginForm = {}; 
     }); 

    }; 

}]); 

angular.module('myApp', ['ngResource']).controller('logoutController', 
    ['$scope', '$location', 'AuthService', 
    function ($scope, $location, AuthService) { 

    $scope.logout = function() { 

     // call logout from service 
     AuthService.logout() 
     .then(function() { 
      $location.path('/login'); 
     }); 

    }; 


/* 

$scope.posts = []; 
    $scope.newPost = {created_by: '', text: '', create_at: ''}; 

    $scope.afficher = function(){ 
     $scope.newPost.created_at = Date.now(); 
     $scope.posts.push($scope.newPost); 
     $scope.newPost = {created_by: '', text: '', created_at: ''}; 
    }; 

*/ 
/** 
    var Meetup = $resource('/api/meetups'); 

    Meetup.query(function (results) { 
    $scope.meetups = results; 
    }); 

    $scope.meetups = [] 

    $scope.createMeetup = function() { 
    var meetup = new Meetup(); 
    meetup.name = $scope.meetupName; 
    meetup.$save(function (result) { 
     $scope.meetups.push(result); 
     $scope.meetupName = ''; 
    }); 
    } 


*/ 



}]); 

angular.module('myApp', ['ngResource']).controller('registerController', 
    ['$scope', '$location', 'AuthService', 
    function ($scope, $location, AuthService) { 

    $scope.register = function() { 

     // initial values 
     $scope.error = false; 
     $scope.disabled = true; 

     // call register from service 
     AuthService.register($scope.registerForm.username, $scope.registerForm.password) 
     // handle success 
     .then(function() { 
      $location.path('/login'); 
      $scope.disabled = false; 
      $scope.registerForm = {}; 
     }) 
     // handle error 
     .catch(function() { 
      $scope.error = true; 
      $scope.errorMessage = "Something went wrong!"; 
      $scope.disabled = false; 
      $scope.registerForm = {}; 
     }); 

    }; 

}]); 

私のコードはこのような私は空白のページを取得します。しかし、もし私が['ngResource']を宣言から取り除くと、evrythingは正常に動作します。助けることができます

+0

angular.module( 'myApp'、['ngResource'])は一度だけ使用してください。あなたの.jsで が angular.module( 'て、myApp'、[ 'ngResource']) ファイル、あなたはangular.module( 'MyApp]を')使用してモジュールをのみを必要とします。 例: (app.js) angular.module( 'myApp'、['ngResource']); MY-resource.jsで angular.module( 'て、myApp') .factory( 'myServiceという'、関数($リソース){ })。 – jcamelis

+0

あなたの返信のためのこんにちは10q詳細に行くことができる私は角度 –

答えて

1
// here you define the module with moduleName as 1st argument and dependencies as 2nd argument. 
angular.module('myApp', ['ngResource']); 

// When you call angular.module('myApp') with just the 1st argument (module name), it returns you the module already defined. 

// here you declare a controller in the module already defined. 
angular.module('myApp') 
    .controller('loginController', 
    ['$scope', '$location', 'AuthService', 
    function ($scope, $location, AuthService) { 

    $scope.login = function() { 

     // initial values 
     $scope.error = false; 
     $scope.disabled = true; 

     // call login from service 
     AuthService.login($scope.loginForm.username, $scope.loginForm.password) 
     // handle success 
     .then(function() { 
      $location.path('/'); 
      $scope.disabled = false; 
      $scope.loginForm = {}; 
     }) 
     // handle error 
     .catch(function() { 
      $scope.error = true; 
      $scope.errorMessage = "Invalid username and/or password"; 
      $scope.disabled = false; 
      $scope.loginForm = {}; 
     }); 

    }; 

}]); 

// here you declare another controller in the module already defined. 
angular.module('myApp') 
    .controller('logoutController', 
    ['$scope', '$location', 'AuthService', 
    function ($scope, $location, AuthService) { 

    $scope.logout = function() { 

     // call logout from service 
     AuthService.logout() 
     .then(function() { 
      $location.path('/login'); 
     }); 

    }; 


/* 

$scope.posts = []; 
    $scope.newPost = {created_by: '', text: '', create_at: ''}; 

    $scope.afficher = function(){ 
     $scope.newPost.created_at = Date.now(); 
     $scope.posts.push($scope.newPost); 
     $scope.newPost = {created_by: '', text: '', created_at: ''}; 
    }; 

*/ 
/** 
    var Meetup = $resource('/api/meetups'); 

    Meetup.query(function (results) { 
    $scope.meetups = results; 
    }); 

    $scope.meetups = [] 

    $scope.createMeetup = function() { 
    var meetup = new Meetup(); 
    meetup.name = $scope.meetupName; 
    meetup.$save(function (result) { 
     $scope.meetups.push(result); 
     $scope.meetupName = ''; 
    }); 
    } 


*/ 

}]); 
// here you declare another controller in the module already defined. 
angular.module('myApp') 
    .controller('registerController', 
    ['$scope', '$location', 'AuthService', 
    function ($scope, $location, AuthService) { 

    $scope.register = function() { 

     // initial values 
     $scope.error = false; 
     $scope.disabled = true; 

     // call register from service 
     AuthService.register($scope.registerForm.username, $scope.registerForm.password) 
     // handle success 
     .then(function() { 
      $location.path('/login'); 
      $scope.disabled = false; 
      $scope.registerForm = {}; 
     }) 
     // handle error 
     .catch(function() { 
      $scope.error = true; 
      $scope.errorMessage = "Something went wrong!"; 
      $scope.disabled = false; 
      $scope.registerForm = {}; 
     }); 

    }; 

}]); 
+0

10qあなたの貴重な時間のために新しいです! –

+0

よろしくお願いします! – jcamelis

関連する問題