我的应用程序中有便捷的服务。
class Rectangle
{
public double Length { get; set; }
public double Width { get; set; }
public double GetArea()
{
return Length * Width;
}
public override string ToString()
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine("Length: " + Length);
stringBuilder.AppendLine("Width: " + Width);
stringBuilder.Append("Area: " + GetArea());
return stringBuilder.ToString();
}
}
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
Console.WriteLine("Length= ");
double.TryParse(Console.ReadLine(), out r.Length);
Console.WriteLine("Width= ");
double.TryParse(Console.ReadLine(), out r.Width);
Console.Writeline("Area= " + r.GetArea());
Console.WriteLine(r.ToString());
Console.ReadLine();
}
}
我想在控制器中使用它
var app = angular.module("appTest", []);
app.service('AuthService', function ($scope) {
$scope.write = function(){
console.log("service")
};
});
但是我有误http://prntscr.com/mckff7
无论如何我都为我服务。结果相同。
我通过http://prntscr.com/mckgrt方式添加我的服务
答案 0 :(得分:2)
您没有将AuthService注入到控制器中。您将其作为一个对象接收,但是您也需要在字符串数组中声明它才能实际注入它。
您的控制器代码应如下所示:
var app = angular.module('appTest', []);
app.service('AuthService', function ($scope) {
$scope.write = function(){
console.log('hello world');
};
});
app.controller("LoginController", ['$scope', '$http', '$cookies',
'$cookieStore', '$rootScope', '$location', 'AuthService',
function ($scope, $http, $cookies, $cookieStore, $rootScope, $location,
AuthService) {
AuthService.write();
}]);