更新$ scope不会影响视图

时间:2016-01-11 18:22:28

标签: javascript angularjs scope angularjs-scope

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

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

这是我的控制者:

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}}">

但即使数据成功加载,我仍然会看到加载消息。

2 个答案:

答案 0 :(得分:3)

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

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

请参阅ng-showng-hide的文档。

答案 1 :(得分:2)

没有进行using Microsoft.Reporting.WinForms; using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Printing; using System.IO; public static class LocalReportExtensions { public static void Print(this LocalReport report) { var pageSettings = new PageSettings(); pageSettings.PaperSize = report.GetDefaultPageSettings().PaperSize; pageSettings.Landscape = report.GetDefaultPageSettings().IsLandscape; pageSettings.Margins = report.GetDefaultPageSettings().Margins; Print(report, pageSettings); } public static void Print(this LocalReport report, PageSettings pageSettings) { string deviceInfo = $@"<DeviceInfo> <OutputFormat>EMF</OutputFormat> <PageWidth>{pageSettings.PaperSize.Width * 100}in</PageWidth> <PageHeight>{pageSettings.PaperSize.Height * 100}in</PageHeight> <MarginTop>{pageSettings.Margins.Top * 100}in</MarginTop> <MarginLeft>{pageSettings.Margins.Left * 100}in</MarginLeft> <MarginRight>{pageSettings.Margins.Right * 100}in</MarginRight> <MarginBottom>{pageSettings.Margins.Bottom * 100}in</MarginBottom> </DeviceInfo>"; Warning[] warnings; var streams = new List<Stream>(); var currentPageIndex = 0; report.Render("Image", deviceInfo, (name, fileNameExtension, encoding, mimeType, willSeek) => { var stream = new MemoryStream(); streams.Add(stream); return stream; }, out warnings); foreach (Stream stream in streams) stream.Position = 0; if (streams == null || streams.Count == 0) throw new Exception("Error: no stream to print."); var printDocument = new PrintDocument(); printDocument.DefaultPageSettings = pageSettings; if (!printDocument.PrinterSettings.IsValid) throw new Exception("Error: cannot find the default printer."); else { printDocument.PrintPage += (sender, e) => { Metafile pageImage = new Metafile(streams[currentPageIndex]); Rectangle adjustedRect = new Rectangle( e.PageBounds.Left - (int)e.PageSettings.HardMarginX, e.PageBounds.Top - (int)e.PageSettings.HardMarginY, e.PageBounds.Width, e.PageBounds.Height); e.Graphics.FillRectangle(Brushes.White, adjustedRect); e.Graphics.DrawImage(pageImage, adjustedRect); currentPageIndex++; e.HasMorePages = (currentPageIndex < streams.Count); e.Graphics.DrawRectangle(Pens.Red, adjustedRect); }; printDocument.EndPrint += (Sender, e) => { if (streams != null) { foreach (Stream stream in streams) stream.Close(); streams = null; } }; printDocument.Print(); } } } 。根据{{​​3}}文档

{{}}

以下是 ng-show