控制器返回數據,但是我無法在HTML文件中迭代它。但是,使用表達式{{電話}}直接顯示返回的數據。我相信數據是以字符串而不是json的形式返回的。我在期待與循環中返回的所有數據相對應的名稱和片段。
<body ng-app="phonecatApp" ng-controller="PhoneListCtrl" >
Search: <input ng-model="query">
Sort By:
<select ng-model="orderByFilter">
<option value="name">Alphabetical</option>
<option value="age">Numerical</option>
</select>
<ul>
<li ng-repeat="phone in phones | orderBy:orderByFilter">
<span>{{phone.name}}</span>
<span>{{phone.snippet}}</span>
</li>
</ul>
<script src="~/Scripts/AngularScript.js"></script>
</body>
角js腳本
var phonecatApp = angular.module('phonecatApp', []);
phonecatApp.controller('PhoneListCtrl', function ($scope, $http) {
$http.get('Home/GetJson').success(function (data){
$scope.phones = data;
});
});
控制器的操作方法
public JsonResult GetJson()
{
string variablename = "[{'name': 'Nexus S','snippet': 'Fast just got faster with Nexus S.', 'age': 1}]";
return Json (variablename, JsonRequestBehavior.AllowGet);
}