我試圖向函數注入依賴關係,但它似乎沒有工作。我做錯了什麼,或者我應該做一些不同的事情,而不是試圖向函數中注入依賴關係?我試圖讓以下代碼工作:如何將依賴關係注入函數中?
angular.module('app').controller('usersController', usersController);
usersController.$inject = ['$http', '$cookies'];
function usersController($http, $cookies) {
var self = this;
self.isLoggedIn = ($cookies.get('token') && $cookies.get('secret'));
register.$inject = ['$http', '$cookies'];
self.register = register;
function register($http, $cookies) {
console.log(self.username);
$http.post('/register', {
username: self.username,
password: self.password,
email: self.email
}).then(function successCallback(response) {
self.isLoggedIn = true;
$cookies.put('token', response.data.token);
$cookies.put('secret', response.data.secret);
}, function errorCallback(response) {
console.log('Something went wrong.');
});
};
};
是的,這工作!謝謝! – Jeffrey