gpt4 book ai didi

javascript - Angular ngResource 不适用于 Controller

转载 作者:行者123 更新时间:2023-12-03 05:48:57 27 4
gpt4 key购买 nike

大家好,我遇到了 Angular 问题。我有一个 SPA, Angular 代码如下:

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'] ,一切都可以正常工作。你能帮忙吗

最佳答案

// 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 = {};
});

};

}]);

关于javascript - Angular ngResource 不适用于 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40224733/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com