我有一个来自后端的JSON响应,我想显示到前端,所以在下面的情况下,我无法将$scope
值绑定到view.If我将响应更改为对象而不是数组其填充。如何将数组对象填充到视图中?
main.html中
<div class="row">
<div class="form-group col-md-6">
<div class="col-md-3">
<label for="workerName">Full Name:</label>
</div>
<div class="col-md-9">
<input type="text" class="form-control" name="workerName" ng-model="data.firstName">
</div>
</div>
<div class="form-group col-md-6">
<div class="col-md-3">
<label for="workerName">Address:</label>
</div>
<div class="col-md-9">
<input type="text" class="form-control" name="workerName" ng-model="data.lastName">
</div>
</div>
</div>
app.js
var Obj = [{firstName: "Mike", lastName:"wegner"},{firstName:"John",lastName:"Ruch"}];
app.get('/test', function (req, res) {
res.send(Obj);
});
workerFacotry.js
angular.module('myApp').factory('workerFactory', function ($http) {
'use strict';
return {
getData: function(){
return $http.get('/test');
}
}
});
workerController.js
angular.module('myApp').controller('WorkerController', function ($scope,workerFactory) {
'use strict';
$scope.test = function(){
alert("first functiona is working");
};
$scope.data = [];
$scope.getTestData = function(){
workerFactory.getData().then(function(response){
$scope.data = response.data;
console.log("Data from server",$scope.data);
})
}
});
Json.js
[{
"firstName": "Mike",
"lastName": "wegner"
}, {
"firstName": "John",
"lastName": "Ruch"
}]
答案 0 :(得分:1)
使用ngRepeat
指令循环遍历data
:
<div class="row" ng-repeat="d in data">
<div class="form-group col-md-6">
<div class="col-md-3">
<label for="workerName">Full Name:</label>
</div>
<div class="col-md-9">
<input type="text" class="form-control" name="workerName" ng-model="d.firstName">
</div>
</div>
<div class="form-group col-md-6">
<div class="col-md-3">
<label for="workerName">Address:</label>
</div>
<div class="col-md-9">
<input type="text" class="form-control" name="workerName" ng-model="d.lastName">
</div>
</div>
</div>