无法在html(视图)中重新加载数据
仅在第一次(页面加载)时加载正常,按钮单击它不会反映更改,但控制器具有正确的记录。
<div class="well" ng-app="myQuizApp"> <fieldset ng-repeat="Question in ::qstns">
</fieldset> </div>
<button type="button" class="btn btn-primary btn-lg btn-block" ng-click="Load_Quizes('2')">Review</button>
myApp.controller("myQuizs", function ($scope, angularService, $window, $sce,$interval) {
var action = 'qiz1';
Load_Quizes(1); // Triggering page load (working fine)
// While calling the function in button click , it gets triggered and contains correct data but ng-repeat does not updates (holds old records) .
function Load_Quizes(qtnserial) {
var getQuzData = angularService.LoadQuizes(action, qtnserial);
getQuzData.then(function (response) {
try {
//LOAD QUESTION CHOICE
if (response.data != null) {
debugger;
$scope.Quiz = response.data;
if ($scope.Quiz.Quiz_id > 0) {
$scope.qst_toask = $scope.Quiz.Quiz_Name.split('"').join('');
$scope.qstns = $scope.Quiz.QuizQuestiones;
} else {
$window.alert("Online mock test / Quiz does not exists.");
}
}
else {
$window.alert("Online mock test / Quiz does not exists.");
}
}
catch (e) {
$window.alert('Quiz response error.');
}
}, function () {
alert('Error in getting records.');
});
}
请帮忙。 谢谢。
答案 0 :(得分:1)
您需要在$scope
上定义Load_Quizes函数。 Angular使用$scope
将视图绑定到控制器。同时从::
中删除ng-repeat="Question in ::qstns"
,因为它用于单向绑定。如果使用单向绑定,控制器中发生的qstns的任何更改都不会反映在视图中。
$scope.Load_Quizes(1);
$scope.Load_Quizes(qtnserial) {
var getQuzData = angularService.LoadQuizes(action, qtnserial);
getQuzData.then(function (response) {
try {
//LOAD QUESTION CHOICE
if (response.data != null) {
debugger;
$scope.Quiz = response.data;
if ($scope.Quiz.Quiz_id > 0) {
$scope.qst_toask = $scope.Quiz.Quiz_Name.split('"').join('');
$scope.qstns = $scope.Quiz.QuizQuestiones;
} else {
$window.alert("Online mock test / Quiz does not exists.");
}
}
else {
$window.alert("Online mock test / Quiz does not exists.");
}
}
catch (e) {
$window.alert('Quiz response error.');
}
}, function () {
alert('Error in getting records.');
});
}