如何使用angularJS在视图中显示工厂方法值

时间:2016-07-01 07:16:07

标签: javascript angularjs json

  

嗨,在注入工厂时,根据我的需要获取Json数据   控制器,我无法在angularJs中显示数据,即   用户界面中的正确答案div,如果有人可以帮助我,我将感激不尽

     

控制器代码:

 public JsonResult displayCorrectAnswer()
        {
            Db.Configuration.ProxyCreationEnabled = false;
         var rs =   from q in Db.questions
                    join a in Db.answers on q.Qid equals a.questionID
                    where a.isAnswer == "1"
                        select new { a.answer1 };
         return Json(rs, JsonRequestBehavior.AllowGet);
        }

>Json Data:

[
{"answer1":"4"},
{"answer1":"Modi"}
]

>Script Code:

var myApp = angular.module("QuestionDisplayModule", [])
        .controller("QuestionDisplayController", function ($scope, $http, $log, answerService) {
            $http.get("displayQuestion").then(function(response)
            {
                $log.info(response);
                $scope.questionsData = response.data;
            })
            $scope.answerCheck = answerService.choose();
        })
        myApp.factory("answerService",  function ($http) {
            var CorrectAnswers = {};
            CorrectAnswers.choose = function () {
                return $http.get("displayCorrectAnswer").success(function (response) {
                    response.data;
                })  
            }
            return CorrectAnswers;
        });
>View Code:

<body ng-app="QuestionDisplayModule">
    <form>
        <div ng-controller="QuestionDisplayController">
            <div ng-repeat="q in questionsData" style="border:groove 1px green">
          <h4 style="color: darkmagenta">{{q.QText}}</h4>
                <p ng-repeat="a in q.answer1" style="color: cadetblue"><input type="radio" name="answers+{{$parent.$index}}" ng-model="q.selectedAns" value="{{a.option1}}" />{{a.option1}}</p>
                <div ng-show="q.selectedAns">your answer is : {{q.selectedAns}}</div>
                <div > correct Answer is : {{answerCheck.answer1}}</div>   
            </div>
                </div>
    </form>
</body>

1 个答案:

答案 0 :(得分:0)

var myApp = angular.module("QuestionDisplayModule", [])
        .controller("QuestionDisplayController", function ($scope, $http, $log, answerService) {
            $http.get("displayQuestion").then(function(response)
            {
                $log.info(response);
                $scope.questionsData = response.data;
            })
            $scope.answerCheck = answerService.choose().then(function(response) {
     $scope.answerCheck = response;
});
        })
        myApp.factory("answerService",  function ($http) {
            var CorrectAnswers = {};
            CorrectAnswers.choose = function () {
                return $http.get("displayCorrectAnswer")
        });
>