我正在尝试使用AngularJS和用VB.NET编写的Web API(为我的实习做这个)。AngularJS - Factory not working/activating
我有我的angularApp定义,我的控制器和工厂一样。 我也在使用Angular的路由,并且这个工作完美。 我正在处理的问题是我的工厂没有激活或不工作。
请看看下面的代码:
我AngularApp.js
var angularApp = angular.module('AngularApp', ['ngRoute']);
angularApp.config(['$routeProvider',
function ($routeProvider) {
$routeProvider
.when('/ExpenseOverview', {
controller: 'ExpenseController',
templateUrl: 'Views/ExpenseOverview.aspx'
})
.when('/AddExpense',
{
controller: 'ExpenseController',
templateUrl: 'Views/AddExpense.aspx'
})
.otherwise({ redirectTo: '/ExpenseOverview' });
}]);
我ExpenseController:
angular.module("AngularApp").controller("ExpenseController", ["$scope", "ExpenseFactory", function ($scope, ExpenseFactory) {
//variabelen
$scope.expenses = [];
$scope.id = 0;
$scope.date = "";
$scope.type = "";
$scope.title = "";
$scope.project = "";
$scope.status = "";
$scope.img = "";
var shown = false;
var dataURL = "";
ExpenseFactory.getList($scope);
}]);
到目前为止,我的控制器没有做远远超过其他通过Web API从数据库中检索数据列表。
我ExpenseFactory.js
angular.module("AngularApp").factory("ExpenseFactory", ["$http", function ($http) {
alert("test factory");
var factory = {};
//lijst van expenses ophalen
factory.getList = function ($scope) {
$http.post("/api/Expense/List")
.success(function(data) {
if (data === undefined || data == "") {
data = [];
}
$scope.expenses = data;
$scope.id = $scope.expenses[$scope.expenses.length - 1].Id + 1;
})
.error(function() {
alert("Er is een fout opgetreden");
});
};
factory.saveList = function(expenseList) {
$http.post("/api/Expense/SaveList", { 'expenses': expenseList })
.success(function() {
alert("Expenses have been saved succesfully!");
})
.error(function() {
alert("Something went wrong while saving the expenses");
});
};
return factory;
}]);
正如你所看到的,我已经把警报后的代码在工厂第一线。这个警报甚至没有弹出,这意味着工厂没有启动/工作。
这段代码失败了什么?
编辑 我将代码更新为当前版本,包含所有关于可能干扰代码的事情的评论。我可以证实,这一切都没有奏效,所以错误发生在别的地方。
另一个说明:我正在使用Visual Studio 2012,如果这可能与它有关,请详细说明我如何修复这个shenannigans。
是否因为“expenseFactory”是否区分大小写。不知道 – Reena
你必须在其他文件中使用'angular.module('AngularApp')'来引用模块 – mohamedrias
@mohamedrias为什么它是必需的,我认为他已经声明全局变量.. –