gpt4 book ai didi

javascript - 更新 $scope 不影响 View

转载 作者:行者123 更新时间:2023-11-30 08:33:19 25 4
gpt4 key购买 nike

我正在尝试从 promise 回调中更新 $scope.loading。我尝试使用 $scope.$apply 但我无法让它工作。

loading 是我 View 中使用的一个变量,用于在数据尚未加载时显示加载条,否则显示数据。

这是我的 Controller :

angular.module('ecommerce')
.controller('ProductsCtrl', function ($scope, ProductsService) {
$scope.loading = true;
$scope.error = false;
ProductsService.getProducts().then(function (data) {
$scope.loading = false;
$scope.products = data;
}, function (response) {
$scope.products = null;
});
});

这是我的观点:

<div ng-show="{{loading}}">Loading... please </div>
<div ng-hide="{{loading}}">

但我一直看到加载消息,即使数据已成功加载也是如此。

最佳答案

去掉 {{}}。您的 ng-showng-hide 指令应如下所示:

<div ng-show="loading">Loading... please </div>
<div ng-hide="loading">

请参阅 ng-show 的文档和 ng-hide .

关于javascript - 更新 $scope 不影响 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34728393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com