【问题标题】:ng-bind-html does not update the record angularjs 1.6ng-bind-html 不更新记录 angularjs 1.6
【发布时间】:2018-06-05 00:46:36
【问题描述】:

无法在 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>

Anguar js 库在此处引用 1.6

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.');
        });
    }

请帮忙。 谢谢。

【问题讨论】:

  • ng-click="Load_Quizes('2')"

标签: jquery angularjs angularjs-scope


【解决方案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.');
    });
}

【讨论】:

  • $scope.Load_Quizes(1);加载完美,问题是单击按钮时 - > Load_Quizes 也被调用并具有正确的 JSON 数据..但数据没有反映在 ng-repeat
  • 尝试从 qstns 中删除 :: 因为它用于单向绑定
  • 谢谢@Vivz 从最近三天开始我一直在努力。
  • 很高兴我能帮上忙。如果它对您有帮助,请为您投票并祝您节日快乐:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-10
  • 2013-09-26
  • 2013-06-18
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多