首页 \ 问答 \ AngularJS自定义过滤器调用两次并在第二次调用时删除输入数据(AngularJS custom filter called twice and delete input data on second call)

AngularJS自定义过滤器调用两次并在第二次调用时删除输入数据(AngularJS custom filter called twice and delete input data on second call)

这是代码。

var app = angular.module("nameApp", ["ngRoute"]);

app.controller("ctrlname", function ($scope, $http, $filter, apiKey, apiUrl) {
    $scope.data = {};
    $scope.currentPage = 1;
    $scope.pageSize = 5;
});

$ scope.data将包含来自HTTP GET请求的数据数组。 以下是用于分页结果的自定义过滤器的代码。 基本上,这会将结果限制为仅5.分页按钮将更新$ scope.currentPage的值。

app.filter("limitResults", function ($filter, $log) {
    return function (data, page, size) {
        if (angular.isArray(data) & angular.isNumber(page) && angular.isNumber(size)) {
            var startPage = (page - 1) * size;
            if (data.length < startPage) {
                return [];
            } else {
                $log.info(data);
                $log.info(page);
                $log.info(size);
                $log.info(startPage);
                return $filter("limitTo")(data.splice(startPage), size);
            }
        } else {
            return data;
        }
    }
});

这是将呈现数据的HTML页面。

<div class="row resultItems" ng-repeat="video in data.videos | limitResults:currentPage:pageSize">
    <div class="col-sm-3 testing">
        <img ng-src="{{video.snippet.thumbnails.default.url}}">
    </div>
    <div class="col-sm-9 testing">
        <h5>
            {{video.snippet.title}}
        </h5>
        <p>
            {{video.snippet.channelTitle}}
        </p>
        <p>
            {{video.snippet.description}}
        </p>
    </div>
</div>

我在自定义过滤器中放了几行$ log.info代码,以便了解应用过滤器时到底发生了什么。 过滤器运行两次,这是正常行为。

我发现令人困惑的是,当自定义过滤器第一次运行时,$ log.info(data)将从HTTP GET调用接收的原始数据记录到控制台。 但是,当自定义筛选器第二次运行时,$ log.info(data)会将一个空数组记录到控制台。

鉴于“$ log.info(data); $ log.info(page); $ log.info(size);” 登录到控制台,显然第二个IF语句(if(data.length <startPage))被评估为TRUE和过滤器(返回$ filter(“limitTo”)(data.splice(startPage),size) ;) 被申请;被应用。

我只是不明白为什么数组,即传递给自定义过滤器的数据,在过滤器第二次运行时被清空。


Here are the codes.

var app = angular.module("nameApp", ["ngRoute"]);

app.controller("ctrlname", function ($scope, $http, $filter, apiKey, apiUrl) {
    $scope.data = {};
    $scope.currentPage = 1;
    $scope.pageSize = 5;
});

The $scope.data will contain an array of data from an HTTP GET request. The following is a code for a custom filter for the purpose of pagination of results. Basically, this will limit the results to only 5. Buttons for pagination will update $scope.currentPage's value.

app.filter("limitResults", function ($filter, $log) {
    return function (data, page, size) {
        if (angular.isArray(data) & angular.isNumber(page) && angular.isNumber(size)) {
            var startPage = (page - 1) * size;
            if (data.length < startPage) {
                return [];
            } else {
                $log.info(data);
                $log.info(page);
                $log.info(size);
                $log.info(startPage);
                return $filter("limitTo")(data.splice(startPage), size);
            }
        } else {
            return data;
        }
    }
});

This is the HTML page that will render the data.

<div class="row resultItems" ng-repeat="video in data.videos | limitResults:currentPage:pageSize">
    <div class="col-sm-3 testing">
        <img ng-src="{{video.snippet.thumbnails.default.url}}">
    </div>
    <div class="col-sm-9 testing">
        <h5>
            {{video.snippet.title}}
        </h5>
        <p>
            {{video.snippet.channelTitle}}
        </p>
        <p>
            {{video.snippet.description}}
        </p>
    </div>
</div>

I put a few lines of $log.info code in the custom filter in order to see what really happens when the filter is applied. The filter runs twice, which is a normal behaviour.

What I find confusing is that when the custom filter runs for the first time, $log.info(data) logs the original data received from a HTTP GET call to the console. However, when the custom filter runs for the second time, $log.info(data) logs an empty array to the console.

Given the fact that "$log.info(data); $log.info(page); $log.info(size);" get logged to the console, it is obvious that the second IF statement (if (data.length < startPage)) is evaluated to TRUE and the filter (return $filter("limitTo")(data.splice(startPage), size);) is applied.

I just don't understand why the array, which is the data passed to the custom filter, gets emptied when the filter runs the second time.


原文:https://stackoverflow.com/questions/36584663
更新时间:2021-11-05 09:11

最满意答案

我的直觉是,你只是填充率。

你的100个精灵覆盖了多少像素? GPU具有有限的计算像素容量(特别是在混合的情况下 - 你说有alpha,因为它需要读取和写入帧缓冲区)。 如果你生成太多,你的帧速率将受到严重影响。 最糟糕的情况是每个精灵都覆盖整个屏幕,大约是屏幕总像素数的100倍。 (那100倍是我们所谓的透支因素)。

另一个选择是你是着色器。 你的片段着色器做什么? 如果用简单的恒定颜色输出替换它会发生什么?

我不认为几何提交与你的性能问题有关(不是100个精灵)。

最重要的是,要考虑性能,您需要使用性能分析工具。 我不打算对ipad进行编码。 SDK是否提供了分析性能的工具?


My gut feeling is that you're simply fill-rate bound.

How many pixels do your 100 sprites cover ? The GPU has a limited capacity of computing pixels (especially with blending on - you have alpha you said- since it requires reading and writing the framebuffer). And if you generate too many of them, your frame rate will suffer dramatically. the worst case for you would be that each sprite covers your whole screen, incurring ~100x the total pixel count of your screen. (that 100x is what we call the overdraw factor).

Another alternative is that you're shader bound. What does your fragment shader do ? What happens if you replace it with a simple constant color output ?

I don't think that the geometry submission has anything to do with your perf issues (not for 100 sprites).

Bottom line is, to look at performance, you want to use performance analysis tools. I don't code myself against the ipad. Does the SDK provide any tool to analyze perf ?

相关问答

更多
  • 是否使用OpenGL ES 1.1或2.0取决于您想要在应用程序中执行的操作,以及您需要与之兼容的设备数量。 所有的iOS设备都支持OpenGL ES 1.1,其中只有iPhone 3G S和更新的设备(iPhone 3GS,iPhone 4,iPad以及第三代和第四代iPod touch)支持OpenGL ES 2.0。 不过,苹果目前出货的所有iOS设备都与OpenGL ES 2.0兼容。 不支持OpenGL ES 2.0的设备的百分比每天都在下降。 所有的iPad都支持OpenGL ES 2.0,所以 ...
  • 终于找到了解决办法。 当我在每个渲染圆圈后调用glFlush()时,它工作正常。 我没有在不同的纹理通道上渲染每个飞机,它迄今为止完美。 谢谢您的帮助。 Finally found the solution. When i call glFlush() after every rendering circle, it works fine. I no render every plane on a different texture channel, it works perfectly so far. T ...
  • 来自http://shivavg.svn.sourceforge.net/viewvc/shivavg/trunk/src/shPipeline.c?revision=14&view=markup : static void shDrawVertices(SHPath *p, GLenum mode) { int start = 0; int size = 0; /* We separate vertex arrays by contours to properly handle the fill mod ...
  • 我非常依赖翻译和缩放 请注意,如果您在绘图调用之间更改模型视图矩阵,则无法批处理任何内容。 (ES2没有改变这一点)。 Vbo a可从opengl ES 1.1获得。 它们可能适用于您定位的设备。 即使对于ES1.0(ARB_vertex_buffer_object) 你可以用世界空间几何创建一个大的VBO(=用cpu解析缩放和转换)并绘制它。 就我个人而言,即使每次更新这个vbo,它都足够快。 发送成千上万的小电话几乎总是最慢的。 从一个固定的管道移动到一个完整的顶点/片段着色器管线是不容易的。 它需要大 ...
  • 听起来像垃圾收集器运行。 要验证这一点,只需看一下logcat ......生涩的时刻是否与标记为“dalvikvm”的行相关并带有“GC_”? Sounds like the garbage collector running. To verify this, just look at logcat... do the jerky moments correlate to lines tagged "dalvikvm" and lead with "GC_"?
  • 你或许可以说服梅萨这样做。 You may be able to convince Mesa to act as such.
  • 有关视网膜的评论是正确的。 由于2倍分辨率的视网膜屏幕,缩放倍数的组合产生了意想不到的结果。 这导致点精灵在OpenGL 3上渲染比在OpenGL ES 2.0上大16倍。 当在着色器中写入原始值时,Point Sprite实际上大2倍,填充了该区域的4倍。 要纠正扩展问题,从规范中读取它是有帮助的: 点光栅化为每个帧缓冲像素生成一个片段,其中心位于以点(xw; yw)为中心的正方形内,边长等于当前点大小。 The comments were correct about retina. A combinat ...
  • GraphicsDevice.DrawUserIndexedPrimitivesImpl方法中存在问题。 如此处所示,该方法没有考虑索引或顶点偏移。 它还错误地确定要绘制的元素数量。 因此,它将为每个绘制调用绘制索引数组(~12000个索引)。 尚未启用混合,因此黑色背景上的透明纹理无法明确这一点。 通过正确计算索引数组的偏移地址和长度来解决该问题。 protected override void DrawUserIndexedPrimitivesImpl(PrimitiveType primi ...
  • 从3GS开始,OpenGL支持最高2048x2048的纹理。 4S和iPad2支持4096x4096。 但是,如果不是这种情况,那么您的选项围绕打包像素和有损压缩或使用多个纹理,这两个纹理都可以在GLSL中或通过几何体修复(需要付费)。 最明显的选择是上传为四个图块。 您在每个方向上的输出大小1936允许您上传具有一些重叠的纹理,这样您即使使用线性纹理过滤也应该能够获得无缝连接。 假设您只需要水平分割,就可以确保一个纹理的底部两行与另一个纹理的顶部两行相同,并将几何连接放在这两行之间。 其他选项不仅不太明显 ...
  • 我的直觉是,你只是填充率。 你的100个精灵覆盖了多少像素? GPU具有有限的计算像素容量(特别是在混合的情况下 - 你说有alpha,因为它需要读取和写入帧缓冲区)。 如果你生成太多,你的帧速率将受到严重影响。 最糟糕的情况是每个精灵都覆盖整个屏幕,大约是屏幕总像素数的100倍。 (那100倍是我们所谓的透支因素)。 另一个选择是你是着色器。 你的片段着色器做什么? 如果用简单的恒定颜色输出替换它会发生什么? 我不认为几何提交与你的性能问题有关(不是100个精灵)。 最重要的是,要考虑性能,您需要使用性能 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。