首页 \ 问答 \ 将jQuery转换为Angular并优化性能(Convert jQuery to Angular and optimize performance)

将jQuery转换为Angular并优化性能(Convert jQuery to Angular and optimize performance)

我找到了漂亮的“控制台之类”窗口 - http://demos.telerik.com/kendo-ui/autocomplete/events (开始输入框),我正在尝试创建Angular版本。

我的代码如下所示:

angular.module('myApp', [])
  .directive('console', ['$rootScope', function($rootScope) {
    return {
      restrict: 'E',
      template: [
        '<div class="console animated">',
        '<div ng-repeat="event in events" ng-class="{\'error\': event.type == \'error\', \'log\': event.type == \'log\'}">{{event.data}}</div>',
        '</div>'
      ].join(''),
      link: function($scope, element) {

        $scope.events = [];

        $rootScope.$on('Log', function(event, data) {
          //console.log(event, data);
          $scope.events.splice(0, 0,{
            type: 'log',
            data: "Log :: "+data
          });
        });

        $rootScope.$on('Error', function(event, data) {
          //console.log(event, data);
          $scope.events.splice(0, 0,{
            type: 'error',
            data: "Error :: "+data
          });
        });
      }
    };

我可以通过调用以下方法将项添加到我的控制台:

$rootScope.$emit("Error", "Something bad happened");

这是我目前的版本: https//plnkr.co/edit/iLZKa8hPCBSRDV0dhn15?p = preview

最重要的是原始的jQuery版本,在Angular版本的底部。

我缺少的是事件聚合:

在此处输入图像描述

原始版本是灌浆条目和添加计数器是同一事件发生一次更多。

如何将其添加到我的代码中(Angular方式)?

除了转换,我还有两个问题我想问:
- 这是我写的第一个指令之一,如果我犯了任何错误,请告诉我。
- 这个“控制台”将显示许多事件,我想知道如何优化它 - 我应该只显示最近20个条目,还是可以添加虚拟滚动等功能来优化它?


I found nice looking "console like" window - http://demos.telerik.com/kendo-ui/autocomplete/events (start typing in box) and I'm trying to create Angular version.

My code looks like this:

angular.module('myApp', [])
  .directive('console', ['$rootScope', function($rootScope) {
    return {
      restrict: 'E',
      template: [
        '<div class="console animated">',
        '<div ng-repeat="event in events" ng-class="{\'error\': event.type == \'error\', \'log\': event.type == \'log\'}">{{event.data}}</div>',
        '</div>'
      ].join(''),
      link: function($scope, element) {

        $scope.events = [];

        $rootScope.$on('Log', function(event, data) {
          //console.log(event, data);
          $scope.events.splice(0, 0,{
            type: 'log',
            data: "Log :: "+data
          });
        });

        $rootScope.$on('Error', function(event, data) {
          //console.log(event, data);
          $scope.events.splice(0, 0,{
            type: 'error',
            data: "Error :: "+data
          });
        });
      }
    };

And I can add item to my console by calling:

$rootScope.$emit("Error", "Something bad happened");

Here is my current version: https://plnkr.co/edit/iLZKa8hPCBSRDV0dhn15?p=preview

On top is original jQuery version, on bottom in Angular version.

The thing I'm missing is event aggregation:

enter image description here

Original version is groupping entries and adding counter is same event occures more that once.

How can I add this to my code (Angular way)?

Besides conversion I have two more questions I'd like to ask:
-This in one of my first directives I'm writing, if I'm making any mistakes in it please let me know.
-This "console" will show many events and I'd like to know how to optimize it - should I show for example only last 20 entries or can I add functionality like virtual scroll to optimize it a bit?


原文:https://stackoverflow.com/questions/36054240
更新时间:2022-11-05 18:11

最满意答案

在您的CriminalProvider构造函数中,您将未初始化的crimeList( null )提供给超类ArrayAdapter构造函数。 您只是在将空引用传递给超级构造函数后初始化列表。

移动

criminalList = new ArrayList<Criminal>()

到成员变量初始化部分:

private static List<Criminal> criminalList = new ArrayList<Criminal>();

static是一个代码味道,但如果你坚持使用它,你可以检查列表的size() ,看看你是否需要在适配器构造函数中填充它。


In your CriminalProvider constructor you're supplying uninitialized criminalList (null) to the superclass ArrayAdapter constructor. You're only initializing the list after passing the null reference to the super constructor.

Move the

criminalList = new ArrayList<Criminal>()

to the member variable initialization part:

private static List<Criminal> criminalList = new ArrayList<Criminal>();

static is a code smell here but if you insist on using it, you can e.g. check for the list's size() to see whether you need to populate it in the adapter constructor.

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)