Greatym.com Greatym.com - 5 months ago 19
Javascript Question

Angular js: Iterate over list in view (ng-foreach)

I am getting json data in server response and return that to $scope.questions

I want to access this questions data in step1.html file.

app.js

(function () {
"use strict";
var app = angular.module("autoQuote",["ui.router","ngResource"]);

app.config(["$stateProvider","$urlRouterProvider", function($stateProvider,$urlRouterProvider){
$urlRouterProvider.otherwise("/");

$stateProvider
.state("step1", {
url : "/",
templateUrl : "easyquote/step1.html",
controller: "questionsCtrl",
})
.state("step2", {
url : "/step2",
templateUrl : "easyquote/step2.html",
controller: "questionsCtrl",
})
}]
);
}());


autoQuoteCtrl.js

(function () {
"use strict";

angular
.module("autoQuote")
.controller("questionsCtrl",["$scope","$http","$state",questionsCtrl]);

function questionsCtrl($scope,$http,$state) {
$http.get('rc1/getQuestions/' + $state.current.name)
.then(function(response) {
$scope.questions = response.data;
});
}
}());


step1.html

<div ng-controller="autoQuoteCtrl">
<form name="DTOstep1" ng-submit="onSubmit()">
<label>Email: </label><input type="text" name="email" id="email" />
<br><br>

<table ng-repeat="questions in que">
<tr>
<td>{{que.QuestionData._attributeName}}</td>
<td></td>
<tr>
</table>
<input type="submit" value="Save" />
</form>
</div>


code preview can be seen here http://plnkr.co/edit/NEmPfu18tePMZ8keZp2p?p=preview

Answer

Your usage of ng-repeat is the wrong way round. It should be like this:

<table ng-repeat="que in questions">
Comments