Angular js:在视图中迭代列表(ng-foreach)

时间:2016-06-30 07:31:55

标签: javascript angularjs

我在服务器响应中获取json数据并将其返回到$ scope.questions

我想在step1.html文件中访问此问题数据。

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>

2 个答案:

答案 0 :(得分:1)

你对ng-repeat的使用是错误的。它应该是这样的:

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

答案 1 :(得分:0)

HTML

A B prob
1 2 0.1
1 3 0.2
1 4 0.3
2 3 0.1
2 4 0.4