2015-09-18 71 views
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); 
 
      } 
 
     }

我做了同樣的方式多次內部不確定的。但在$http請求後向$scope.ProductCategoryList分配值時拋出js錯誤ReferenceError: $scope is not defined。這裏有什麼問題?我嘗試了很多方法,但無法找到答案。

即使我試圖用這種方式

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!!!'); 
 
     }) 
 
    }; 
 
});

同樣的問題。 $範圍沒有定義

+0

你沒有注入服務。 –

+0

上傳代碼後,我發現它並上傳了正確的代碼。即使現在問題也是如此。我嘗試了很多方法。不使用服務並直接調用get。問題類似 –

回答

0

注入服務在你的控制器,

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

爲什麼我們需要注入$ http?它不是已經注入了服務? –

+0

@ImabAsghar實際上不需要 – Sajeetharan

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; 
    }); 
}]); 
+0

以前添加的服務。如何實際注入$ scope?像工廠('DropDownService',函數($ http,$ scope),但它正在發生注入錯誤 –

+0

爲什麼我們需要注入$ http?是不是已經注入了服務? –

+0

@ImabAsghar,如果產品正在所有來自服務的數據,然後它不會被需要,它控制器正在執行一些自己的行動,在這種情況下,它將需要。 – Rex