2015-04-07 74 views
1

我正在尝试使用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。

+0

是否因为“expenseFactory”是否区分大小写。不知道 – Reena

+0

你必须在其他文件中使用'angular.module('AngularApp')'来引用模块 – mohamedrias

+0

@mohamedrias为什么它是必需的,我认为他已经声明全局变量.. –

回答

0

谢谢@mohamedrias帮助我找到解决方案。 确实是因为URL路径无效。 由于无法加载的视图,工厂正在崩溃。 我不知道这是怎么发生的,但它现在已经修复了! 所有代码都是正确的,除了对我的视图引用无效的URL路径。

感谢您的所有帮助和意见,我会为我的项目即将到来的部分记住一切。

StackOverflow真棒!

+0

很高兴知道:)享受 – mohamedrias

0

你错过了返回从工厂方法$http承诺

factory.getList = function ($scope) { 
    return $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) { 
    return $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"); 
     }); 
}; 

而不是将整个范围,你应该创建一个模型对象像$scope.model = {},并且将举行ng-model(范围变量)的所有值不将全范围传递给工厂,只将相关数据传递给服务方法。

+0

OP要求,'工厂'的第一条语句是alert(“测试工厂”)''这与控制器代码有关,比工厂更重要。 – mohamedrias

+0

由于工厂中的第一行代码执行不当,返回许诺根本没有任何帮助。我不知道工厂被执行时发生了什么,但我知道的是,除了我可能已经忘记的“回归”之外,我的工厂中的所有东西都没有被执行。 – Jorrex

+0

@Jorrex任何控制台错误..使用我的方法? –

0

由于应用程序,控制器和工厂在不同的文件中,最好避免使用angularApp来引用模块。

而是使用以下语法:

angular.module('AngularApp').factory("ExpenseFactory", ["$http", function ($http) { 

这里,

angular.module('AngularApp') 

意味着你得到的模块。

如果你打算使用var angularApp,你实际上用你的变量名称来污染全局命名空间。

另外,如果偶然出现在其他库代码中,某人将其重新分配给其他库,那么您的整个应用程序将会中断。

例如:

in another file, `angularApp = alert;` 

还有一两件事,

在你的控制器:

angularApp.controller("ExpenseController", ["$scope", "ExpenseFactory", function ($scope, expenseFactory) { 

你有expenseFactory一个错字,因为它必须是ExpenseFactory

angularApp.controller("ExpenseController", ["$scope", "ExpenseFactory", function ($scope, ExpenseFactory) { 
+0

仍然无法使用。工厂中的第一行代码甚至没有执行。我尝试了你的代码,并用你给我的语法替换了所有“angularApp”变量。但它没有任何影响:/ – Jorrex

+0

你试过我的最新更新吗?您的控制器声明部分有错字 – mohamedrias

+0

我们在功能中使用的'ExpenseFactory'对象不是caseSensitive –