gpt4 book ai didi

javascript - 使用 Angular 中的服务

转载 作者:行者123 更新时间:2023-11-28 18:30:47 25 4
gpt4 key购买 nike

当我尝试在应用程序的 Controller 中使用 Angular 服务时遇到一些问题。

当我尝试在 Controller 中使用服务功能时,我的控制台向我抛出错误:/

var app = angular.module('app', ['ngRoute'])

app.config(['$routeProvider',
function ($routeProvider) {
$routeProvider
.when('/login', {
controlleur: 'login',
templateUrl: 'modules/login/login.html'
})

.otherwise({
redirectTo: '/login'
});
}]);

app.service('coreServices', [function () {
this.helloConsole = function () {
console.log("console services");
};
}]);

app.controller('loginController', ['$scope', '$http', '$rootScope', '$location', 'coreServices', LoginController]);

function LoginController($scope, $http, $rootScope, coreServices) {
var vm = this;

vm.helloConsole = coreServices.helloConsole;
vm.helloConsole();
}
angular.js:13708 TypeError: vm.helloConsole is not a function
at new LoginController

我将这个 fiddle 链接给您,向您展示我是如何做的:https://jsfiddle.net/h8yaxLap/2/

抛出的错误是:angular.js:13708 TypeError: vm.helloConsole 不是函数 在新的 LoginController

最佳答案

在您的示例中,Angular 会将 $location 映射到函数中注入(inject)参数中的 coreService 。所以我会选择

app.controller('loginController', ['$scope', '$http', '$rootScope', '$location', 'coreServices', LoginController]);
function LoginController($scope, $http, $rootScope, $location, coreServices)

关于javascript - 使用 Angular 中的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38060956/

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