gpt4 book ai didi

javascript - 工厂没有被注入(inject) Controller - angularJS

转载 作者:行者123 更新时间:2023-12-03 11:08:00 25 4
gpt4 key购买 nike

我是 angularJS 的新手。我正在尝试将服务分离到我的应用程序中的单独文件中。一切工作正常,但是当我尝试将工厂对象注入(inject) Controller 时,它不起作用。下面是代码

这是应用程序文件
应用程序.js

'use strict';

var app = angular.module('myApp', ['ngRoute']);
app.config(function ($routeProvider, $controllerProvider,

$compileProvider, $filterProvider, $provide) {
app.controllerProvider = $controllerProvider;
app.compileProvider = $compileProvider;
app.routeProvider = $routeProvider;
app.filterProvider = $filterProvider;
app.provide = $provide;


app.routeResolver = function (basePath, viewName,controllerName, isSecure) {
var routeConf =
{
templateUrl: 'Views/' + basePath + viewName + '.html',
controller: controllerName + 'Controller',
secure: isSecure ? isSecure : false,
resolve: {
deps: function ($q, $rootScope) {
var deferred = $q.defer();
var dependencies =
[
'Controllers/' + basePath + controllerName + 'Controller.js',
];

require(dependencies, function () {
$rootScope.$apply(function () {
deferred.resolve();
});
});

return deferred.promise;
}
}
}
return routeConf;

}

$routeProvider
.when('/', {})
.when('/Admin', app.routeResolver('Admin/', 'Admin','Admin'))
.when('/Customer', app.routeResolver('Customer/', 'Customer','Customer'))
.when('/Teller', app.routeResolver('Teller/', 'Teller','Teller'))
.when('/Finance', app.routeResolver('Finance/', 'Finance','Finance'))
.otherwise({ redirectTo: '/' });
});

这是我的工厂
AuthenticationService.js

'use strict';

app.factory('authenticationService', ['$scope', 'httpService', function ($scope, httpService) {

return {
authenticateUser: function () {
if (!$scope.userName && !$scope.keyWord) {
var result = httpService.postService(
{
username: $scope.userName,
keyword: $scope.keyWord
}, "../api/authenticateUser");
result.then(successCall, errorCall);
}
}
}

successCall =function(dataObject) {

}

errorCall = function (dataObject) {

}

}]);

这是我的 Controller
LoginController.js

'use strict';

app.controller('LoginController', ['$scope','authenticationService', function ($scope, authenticationService) {

$scope.ValidateLogin = function () {
var result = window.confirm("Validate Login Called for the user :" + $scope.userName);
var result1 = authenticationService.authenticateUser();

}
}]);

当我没有注入(inject)身份验证服务时,登录 Controller 工作正常。但是当我尝试注入(inject)身份验证服务时,它不起作用。我在做什么。对你的帮助表示感谢。提前致谢。
注意:我也尝试过将其创建为服务。但这仍然不起作用。

最佳答案

尽管不建议在工厂和服务中访问 $scope,但您可以使用 angular.element(ELEMENT).scope()

但最好让您的身份验证服务接受用户名和密码,并且不对其当前范围进行任何假设,以使其可重用:

authenticateUser: function (username, password) {
...... //
return result.then(successCall, errorCall); // return the promise
}

然后在你的 Controller 中:

$scope.ValidateLogin = function () {
....//
authenticationService.authenticateUser($scope.username, $scope.password)
.then(function(response){
// deal with the response here
});

}

关于javascript - 工厂没有被注入(inject) Controller - angularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27749879/

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