i have json response backend want display front end in below case not able bind $scope
value view.if change response object instead of array populating.how populate array object view ?
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 working"); }; $scope.data = []; $scope.gettestdata = function(){ workerfactory.getdata().then(function(response){ $scope.data = response.data; console.log("data server",$scope.data); }) } });
json.js
[{ "firstname": "mike", "lastname": "wegner" }, { "firstname": "john", "lastname": "ruch" }]
loop through data
using ngrepeat
directive as:
<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>
Comments
Post a Comment