Услуги:Использование AngularJS службы в контроллере
app.service('myService', ['$scope', '$timeout', function($scope, $timeout){
return {
fn: function(messageTitle, messageContent) {
$timeout(function() {
$scope.fadeMessageSuccess = true;
}, 3000);
}
}
}]);
Контроллер:
app.controller("AccountCtrl", ["$scope", "Auth", "$timeout", "myService",
function($scope, Auth, $timeout, myService) {
myService.fn();
$scope.createUser = function() {
$scope.message = null;
$scope.error = null;
// Create a new user
Auth.$createUserWithEmailAndPassword($scope.accountEmailAddress, $scope.accountPassword)
.then(function(firebaseUser) {
$scope.message = "User created with uid: " + firebaseUser.uid;
console.log($scope.message);
}).catch(function(error) {
$scope.error = error;
console.log($scope.error);
});
};
}
]);
Я пытаюсь создать службу, так что я могу использовать функцию в нескольких контроллерах, но я есть проблема получив эту первую работу. Это сообщение об ошибке я получаю в консоли:
angular.js:13550Error: [$injector:unpr]
Использование недопустимого файла angularjs должно содержать более подробное сообщение об ошибке. – Jukebox
См. [Этот пост] (http://stackoverflow.com/questions/22898927/injecting-scope-into-an-angular-service-function). Внедрение '$ scope' в службу не имеет смысла. – user2718281