2015-10-28 46 views
-1

我在MVC5中有一個名爲Employer的區域,並且在根中有一個名爲app的文件夾,其中包含名爲list的文件夾。在列表中的文件夾,我創建js文件,並作爲一個服務工廠我的用戶驗證碼:

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

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

     return { 
      userLists: userLists 
     }; 

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

但newlist和用戶列表的行爲不叫我的控制器變量是不確定的。這是我的控制器代碼:

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

     $scope.userlist = []; 

     $scope.count = 0; 

     $scope.submitForm = function() { 

     }; 

     $scope.loadLists = function() { 
      $scope.userlist = listService.userLists; 
      $scope.d = "ffdgdfg"; 
     }; 

     $scope.updateName = function (newtitle) { 
      $scope.list.Name = newtitle; 
     }; 
    }); 

回答

0

您需要調整您的工廠是這樣的:

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

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

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

...否則,用戶列表將是私有的。

+0

爲什麼我的動作沒有被調用? –

+0

不確定你的意思是「行動」,但你的服務方法都返回$ http的承諾。您可能需要使用「then」來解決這些問題。 –

+0

我需要在MVC5應用程序中調用ListController中的方法/ Areas/Employer/List/newlist。 –

0

您是否嘗試將路由初始化移至區域註冊?

using System.Web.Mvc; 

namespace MyAreaTest 
{ 
    public class MyAreaRegistration : AreaRegistration 
    { 
     public override string AreaName 
     { 
      get { return "MyArea"; } 
     } 

     public override void RegisterArea(AreaRegistrationContext context) 
     { 
      context.MapRoute(
       "Area_default", 
       "MyArea/{controller}/{action}/{id}", 
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
      ); 
     } 
    } 
} 
+0

我創建了一個沒有區域和默認設置的新項目,沒有任何改變。 –