javascript - 如何使用 ng-repeat 访问驻留在函数和循环中的 json 数组?

标签 javascript jquery json angularjs

我的 html 代码如下,用于评论页面:-

<div class="all_ques_back col-md-12" ng-init="result()" ng-repeat="ans in correctAns">
    <div class="col-xs-1 col-md-1"><i class="fa fa-check-square fa-2x col_padd right_ans_font"></i></div>
    <div class="col-xs-9 col-md-10 col_padd">
        <div class="all_ques">hello your ans {{ans}}</div>
    </div>
    <div class="col-xs-1 col-md-1 col_padd"><i class="fa fa-angle-right right_arrow "></i></div>

我的 Controller 代码如下:-

var data = angular.module('App', ['ngRoute']);

data.controller('SmartLearnerController', function($scope, $location) {
    $scope.result = function() {
        $scope.correctAns = [{
            "QuestionID": "1",
            "QuestionLabel": "Why are mirrors often slightly curved (convex) ?",
            "Image": "zibra-crossing.jpg",
            "Correct": "two",
            "Explaination": "Question one explaination is goes here"
        }, {
            "QuestionID": "2",
            "QuestionLabel": "You are about to drive home. You feel very tired and have a severe headache. You should ?",
            "Image": "",
            "Correct": "one",
            "Explaination": "Question two explaination is goes here"
        }, {
            "QuestionID": "3",
            "QuestionLabel": "While you are driving on gra dient roads,you should ?",
            "Image": "sign traffic.jpg",
            "Correct": "one",
            "Explaination": "Question three explaination is goes here"
        }, {
            "QuestionID": "4",
            "QuestionLabel": "When child lock is applied in car ?",
            "Image": "",
            "Correct": "two",
            "Explaination": "Question four explaination is goes here"
        }]
        $location.path("/reviewans");
    }
});

最佳答案

请检查工作示例:DEMO

只需将 ng-init 移至上面的标签即可

<body ng-controller="MainCtrl"  ng-init="result()" >
<div class="all_ques_back col-md-12" ng-repeat="ans in correctAns">
<div class="col-xs-1 col-md-1"><i class="fa fa-check-square fa-2x col_padd right_ans_font"></i></div>
<div class="col-xs-9 col-md-10 col_padd">
    <div class="all_ques">hello your ans {{ans}}</div>
</div>
<div class="col-xs-1 col-md-1 col_padd"><i class="fa fa-angle-right right_arrow "></i></div>
</div>

关于javascript - 如何使用 ng-repeat 访问驻留在函数和循环中的 json 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31403922/

相关文章:

javascript - Google linechart 可视化图表在更新时消失了?

javascript - 为分页导航功能添加延迟

jquery - 使用 $.post 发布数据失败并显示空数据

json - 使用Elephant Bird jsonLoader时Pig Job挂起

javascript - NOT 与 NULL 与本地存储

javascript - 如何以编程方式更改 Leaflet 中的图层?

javascript - 我可以使用它来引用委托(delegate)元素的选择器吗?

javascript - 如何使用 blueimp 文件上传插件只上传一次文件?

javascript - 使用 GET 请求动态填充表

javascript - 如何等待资源加载