2016-09-03 75 views
0

我想呈現從我的角度控制器在視圖中的json結果。但我沒有這樣做。我需要顯示服務中的名稱列表。如果我寫這樣{{菜單中的[0]。名稱}},我得到一個結果,但我需要顯示的名字無法呈現JsonResult的視圖AngularJS

HTML列表

 <table> 
     <tr ng-repeat="menu in MenuAnshu"> 
     <td> 

     {{menu.name}} 
    < /td> 
</tr> 
下面

是我的控制器代碼:

  (function() { 
var EmployeesController = function ($scope, employeeService, $log) { 
    var employees = function (data) { 
     $scope.Employees = data; 

    }; 

    var MenuAnshuData = function (data) { 
     $scope.MenuAnshu = data; 
     console.log(data) 
    }; 

    var errorDetails = function (serviceResp) { 
     $scope.Error = "Something went wrong ??"; 
    }; 
    employeeService.employees().then(employees, errorDetails); 
    employeeService.MenuAnshuData().then(MenuAnshuData, errorDetails); 
    $scope.Title = "Employee Details Page"; 
    $scope.EmployeeName = null; 
}; 

app.controller("EmployeesController", ["$scope", "employeeService", "$log", EmployeesController]); 

}());

從服務我很喜歡這個

  (function() { 
var employeeService = function ($http) { 
    var employees = function() { 
     return $http.get("http://localhost:63352/api/ptemployees") 
        .then(function (serviceResp) { 
         return serviceResp.data; 
        }); 
    }; 

    var MenuAnshuData = function() { 
     return $http.get("http://redshaft.in/ocapi/index.php?route=api/order/getcategories").then(function (serviceResp) { 
      return serviceResp.data; 
     }); 


    }; 
    return { 
     employees: employees,   
     MenuAnshuData: MenuAnshuData 

    }; 
}; 

var module = angular.module("ProjectTrackingModule"); 
module.factory("employeeService", ['$http', employeeService]); 

}())獲取數據;

我覺得我在js方面做錯了什麼。我想通過menu.name來訪問數組,但控制器數據是一個有數組的對象。

JSON結果,我對控制器側

   {"categories":[{"category_id":"20","image":"catalog\/demo\/compaq_presario.jpg","parent_id":"0","top":"1","column":"1","sort_order":"1","status":"1","date_added":"2009-01-05 21:49:43","date_modified":"2011-07-16 02:14:42","language_id":"1","name":"Desktops","description":"&lt;p&gt;\r\n\tExample of category description text&lt;\/p&gt;\r\n","meta_title":"","meta_description":"Example of category description","meta_keyword":"","store_id":"0"},{"category_id":"18","image":"catalog\/demo\/hp_2.jpg","parent_id":"0","top":"1","column":"0","sort_order":"2","status":"1","date_added":"2009-01-05 21:49:15","date_modified":"2011-05-30 12:13:55","language_id":"1","name":"Laptops &amp; Notebooks","description":"&lt;p&gt;\r\n\tShop Laptop feature only the best laptop deals on the market. By comparing laptop deals from the likes of PC World, Comet, Dixons, The Link and Carphone Warehouse, Shop Laptop has the most comprehensive selection of laptops on the internet. At Shop Laptop, we pride ourselves on offering customers the very best laptop deals. From refurbished laptops to netbooks, Shop Laptop ensures that every laptop - in every colour, style, size and technical spec - is featured on the site at the lowest possible price.&lt;\/p&gt;\r\n","meta_title":"","meta_description":"","meta_keyword":"","store_id":"0"},{"category_id":"25","image":"","parent_id":"0","top":"1","column":"1","sort_order":"3","status":"1","date_added":"2009-01-31 01:04:25","date_modified":"2011-05-30 12:14:55","language_id":"1","name":"Components","description":"","meta_title":"","meta_description":"","meta_keyword":"","store_id":"0"},{"category_id":"57","image":"","parent_id":"0","top":"1","column":"1","sort_order":"3","status":"1","date_added":"2011-04-26 08:53:16","date_modified":"2011-05-30 12:15:05","language_id":"1","name":"Tablets","description":"","meta_title":"","meta_description":"","meta_keyword":"","store_id":"0"},{"category_id":"17","image":"","parent_id":"0","top":"1","column":"1","sort_order":"4","status":"1","date_added":"2009-01-03 21:08:57","date_modified":"2011-05-30 12:15:11","language_id":"1","name":"Software","description":"","meta_title":"","meta_description":"","meta_keyword":"","store_id":"0"},{"category_id":"24","image":"","parent_id":"0","top":"1","column":"1","sort_order":"5","status":"1","date_added":"2009-01-20 02:36:26","date_modified":"2011-05-30 12:15:18","language_id":"1","name":"Phones &amp; PDAs","description":"","meta_title":"","meta_description":"","meta_keyword":"","store_id":"0"},{"category_id":"33","image":"","parent_id":"0","top":"1","column":"1","sort_order":"6","status":"1","date_added":"2009-02-03 14:17:55","date_modified":"2011-05-30 12:15:25","language_id":"1","name":"Cameras","description":"","meta_title":"","meta_description":"","meta_keyword":"","store_id":"0"},{"category_id":"34","image":"catalog\/demo\/ipod_touch_4.jpg","parent_id":"0","top":"1","column":"4","sort_order":"7","status":"1","date_added":"2009-02-03 14:18:11","date_modified":"2011-05-30 12:15:31","language_id":"1","name":"MP3 Players","description":"&lt;p&gt;\r\n\tShop Laptop feature only the best laptop deals on the market. By comparing laptop deals from the likes of PC World, Comet, Dixons, The Link and Carphone Warehouse, Shop Laptop has the most comprehensive selection of laptops on the internet. At Shop Laptop, we pride ourselves on offering customers the very best laptop deals. From refurbished laptops to netbooks, Shop Laptop ensures that every laptop - in every colour, style, size and technical spec - is featured on the site at the lowest possible price.&lt;\/p&gt;\r\n","meta_title":"","meta_description":"","meta_keyword":"","store_id":"0"}]} 

我的問題是我不能夠呈現使用{{menu.names}}在視圖名稱列表。我需要做什麼才能獲得視圖中的名稱列表?

+0

向我們顯示您的控制器代碼,您在哪裏填充變量'MenuAnshu' –

+0

請添加您的控制器代碼 –

+0

嘗試在您的視圖中顯示「MenuAnshu」的內容,如下所示:'

{{MenuAnshu | json}}
'。如果這不包含任何數據,那麼這個變量沒有被你的控制器中的任何數據填充。 – cnorthfield

回答

1

您的代碼不易讀。

我建議你從你的服務回報承諾,您的控制器,然後解決它像這樣:

// service 
// return the promise of the GET request 
var MenuAnshuData = function() { 
    return $http.get("http://redshaft.in/ocapi/index.php?route=api/order/getcategories"); 
}; 

// controller 
// make the call to MenuAnshuData from your service 
employeeService.MenuAnshuData().then(function(response) { 

    // log the data so you can inspect what you have 
    console.log(response.data) 

    // pouplate MenuAnshu with the response 
    $scope.MenuAnshu = data; 

}, function(error) { 

    // handle any failed response 

}); 
+0

我已經重建了代碼。我試過我們的代碼。在控制檯我得到運算是這樣的:對象{數據:對象,狀態:200,配置:對象,狀態文本: 「OK」} 配置 : 對象 數據 : 對象 類別 : 陣列[8] : 對象 CATEGORY_ID :通過使用我已經寫在HTML代碼我不能達到所需結果 「20」我怎樣才能呈現此視圖上.., – pankaj

+0

我已編輯了響應對象 – cnorthfield

+0

我得到這個響應對象{categories:Array [8]} 類別 : 陣列[8] : 對象 CATEGORY_ID : 「20」 柱 : 「1」 DATE_ADDED : 「2009-01-05 21時49分43秒」 DATE_MODIFIED : 「 2011-07-16 2" 點14分42秒 描述 : 「< p類別描述文本</p的> ↵\t >例↵ 」 圖像0​​: 「目錄/演示/ compaq_presario.jpg」 LANGUAGE_ID : 「1」 meta_description : meta_keyword 「類別描述的實施例」: 「」 meta_title : 「」 名稱 : 控制檯中的「桌面」... – pankaj

0

如果你想遍歷的角度JS對象,你必須做如下聲明:

Ng-repeat="(key,value) in object" 

試試看,我已經看到該URL在第一時間檢索json對象,然後我認爲可以工作。

評論結果!

+0

可以詳細說明...不知道如何使用(鍵,值)在我的$範圍對象 – pankaj

0

我相信這個問題是這一行: employeeService.MenuAnshuData().then(MenuAnshuData, errorDetails);

您可以更新您的服務如下:

var MenuAnshuData = function() { 
    var config = {method: 'GET', url:"http://redshaft.in/ocapi/index.php?route=api/order/getcategories"} 
    return $http(config); 
}; 

,然後訪問承諾的get方法在你的控制器:

employeeService.MenuAnshuData().then(MenuAnshuData, errorDetails); 
+0

嘗試這給了我errorangular.js:12722錯誤:[$ http:badreq] http://錯誤.angularjs.org/1.4.9/$ http/badreq?p0 = http%3A%2F%2Fredshaft.in%2Focapi%2Findex.php%3Froute%3Dapi%2Forder%2Fgetcategories – pankaj

+0

嘗試更新的答案 – Sayed

+0

它給了我這個json對象類別 : 陣列[8] : 對象 CATEGORY_ID : 「20」 柱 : 「1」 DATE_ADDED : 「2009-01-05 21時49分43秒」 DATE_MODIFIED : 「2011-07-16二時14分42秒」 – pankaj