AngularJS $ http.get调用MVC5动作

时间:2015-10-27 11:30:59

标签: javascript angularjs asp.net-mvc

我是AngularJS的新手。我已经使用AngularJS创建了一个应用程序。我有一个app文件夹,里面有一个文件列表文件夹: List.html和

listcontroller.js

angSalaryApp.controller('listController',
function ListController($scope, ListService) {
    $scope.list = listService.newlist;

    $scope.count = 0;

    $scope.submitForm() = function () {

    };

    $scope.updateName() = function () {
        $scope.count++;
    };
});

listService.js

angSalaryApp.factory('ListService',
    ["$http",
        function ($http) {
            var newList = function(){
                return $http.get("Areas/Employer/List/newlist");
            };
        }
    ]);

listdirective.js

    angSalaryApp.directive("listForm", function () {
    return {
        restrict: "E",
        templateUrl: "app/list/list.html"
    }
});

在app文件夹中,我有一个JS文件SalaryApp.js

var angSalaryApp = angular.module('angSalaryApp',[])

当我的代码调用$ http.get时,我收到了一条错误消息

Error: [$injector:undef] Provider 'ListService' must return a value from $get factory method.
http://errors.angularjs.org/1.4.6/$injector/undef?p0=ListService
   at enforcedReturnValue (http://localhost:5137/Scripts/angular.js:4330:9)
   at invoke (http://localhost:5137/Scripts/angular.js:4476:7)
   at Anonymous function (http://localhost:5137/Scripts/angular.js:4293:13)
   at getService (http://localhost:5137/Scripts/angular.js:4435:11)
   at invoke (http://localhost:5137/Scripts/angular.js:4464:9)
   at Anonymous function (http://localhost:5137/Scripts/angular.js:9127:11)
   at nodeLinkFn (http://localhost:5137/Scripts/angular.js:8239:13)
   at compositeLinkFn (http://localhost:5137/Scripts/angular.js:7671:13)
   at compositeLinkFn (http://localhost:5137/Scripts/angular.js:7675:13)
   at publicLinkFn (http://localhost:5137/Scripts/angular.js:7546:30)

1 个答案:

答案 0 :(得分:0)

在控制器中,您正在注入ListService,因此请致电ListService.newlist而不是listService.newlist

angSalaryApp.controller('listController',
function ListController($scope, ListService) {
    $scope.list = ListService.newlist;
});

在工厂中,它应该返回object但你没有返回任何object所以通过在返回对象中添加函数返回对象。

angSalaryApp.factory('ListService', ["$http",
  function($http) {
    return {
      newList: newList
    };

    function newList() {
      return $http.get("Areas/Employer/List/newlist");
    }
  }
]);