Fur*_*ran 2 javascript angularjs angular-material
首先,我在 stackoverflow 中查看了有关它的其他问题,并阅读了许多答案,但仍然无法解决我的问题。
我正在使用 Angular Material,但在视图和模型同步方面遇到了问题。
这是我的控制器代码:
$scope.getQuestionByDateRange = function (range) {
QuestionService.getQuestions(1, 'week', 10, function (response) {
$scope.questions = response.data;
})
}
Run Code Online (Sandbox Code Playgroud)
这是我的视图代码:
<ul>
<li ng-repeat="question in questions">{{question.title}}<li>
<ul>
Run Code Online (Sandbox Code Playgroud)
当我更新$scope.questions控制器中的变量时,我的视图正确呈现模型。
但是当我再次更新时$scope.questions:我的模型得到更新(我可以看到控制台日志中的更改)但视图没有。
我对此进行了研究,并找到了诸如$scope.$apply, 之类的解决方案$scope.$digest,$scope.$evalAsync但无法解决我的问题。
怎么了?
这里的问题是整个对象都被替换了(因此它失去了与视图的绑定)。
我会而不是替换整个对象;只需替换它的元素。
$scope.questions = $scope.questions || [];
$scope.getQuestionByDateRange = function (range) {
QuestionService.getQuestions(1, 'week', 10, function (response) {
$scope.questions.length=0; // Reset the array
$scope.questions.push.apply($scope.questions, response.data);
})
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2148 次 |
| 最近记录: |