在$ http call

时间:2015-09-18 05:21:34

标签: javascript angularjs c#-4.0

我的角度代码



angular.module('MyApp').
controller('ProductController', function ($scope, DropDownService) {
    $scope.Product = {};
    $scope.ProductCategoryList = null;
    DropDownService.GetCategory().then(function (d)
    {
        $scope.ProductCategoryList = d.data;
    });
}).
factory('DropDownService', function ($http) {
    var fac = {};
    fac.GetCategory = function() {
        return $http.get('/Product/GetAllCategory');
    };
    return fac;
});




我的服务器端



public JsonResult GetAllCategory()
        {
            
            //List<tblCategory> categories = new List<tblCategory>();
            try
            {
                using(CurtainHomesDBEntities  dc = new CurtainHomesDBEntities())
                {
                     var categories = dc.tblCategory.Select(a => new { a.Id, a.CatagoryName }).ToList();
                     return Json(new { data = categories, success = true }, JsonRequestBehavior.AllowGet);
                }
                
            }
            catch(Exception ex)
            {
                return Json(ex);
            }
        }
&#13;
&#13;
&#13;

我多次做同样的事。但在ReferenceError: $scope is not defined请求后将值分配给$scope.ProductCategoryList时,会抛出js错误$http。这里有什么问题?我尝试了很多方法,但无法找到答案。

即便我尝试过这种方式

&#13;
&#13;
angular.module('MyApp').
controller('ProductController', function ($scope, $http) {
    $scope.Product = {};
    $scope.LoadCategory = function () {
        $scope.categoryList = null;

        $http.get('/Product/GetAllCategory/')
        .success(function (data) {
            $scope.categoryList = data.data;
        })
        .error(function (XMLHttpRequest, textStatus, errorThrown) {
            toastr.error(XMLHttpRequest + ": " + textStatus + ": " + errorThrown, 'Error!!!');
        })
    };
});
&#13;
&#13;
&#13;

同样的问题。 $ scope未定义

2 个答案:

答案 0 :(得分:1)

您需要注入$scope以及service

angularModule.controller("ProductController", ["$scope","$http", 'DropDownService', function ($scope, $http, DropDownService) {
    $scope.Product = {};
    $scope.ProductCategoryList = null;
    DropDownService.GetCategory().then(function (d)
    {
        $scope.ProductCategoryList = d.data;
    });
}]);

答案 1 :(得分:0)

在控制器中注入服务,

class dis
{
   var dist = 0

    func run()->Int
    {
       dist += 1
       return dist
    }
 }

 var d = dis()
 print("\(d.run()) \n")  //<--- do this 6 times you will see the result