我是否应该将一次性绑定到组件的 JSON 字符串传递给组件以减少观察者

should I pass a JSON string with one-time binding to a component to reduce watchers

问题:这是一个好的做法,尽管它通过减少观察者来提高性能

使用Angular 1.5。 创建一个 table(无限滚动),其中 几乎每个单元格的内容都是一个组件。

在任何给定时间页面上都有超过 500 名观看者,这会导致向下滚动时呈现延迟。

为了减少绑定,我想始终将数据作为字符串从父组件传递到子组件(当我传递对象时是JSON字符串),一次性绑定

像这样:

//In parent component (table component) controller:

this.name = "bob"
this.info = {city: "ABC", country: "AAA", zip: "100001", location "123 ABC"};
this.JSONStringifiedInfo = JSON.strigify(this.info);

// In parent view
<table>
<tr>
<table-cell-component-1 info={::$ctrl.JSONStringifiedInfo } name={::$ctrl.name}></table-cell-component-1>
</td>...


//and now in table-cell-component-1 controller

...
 bindings: {
    info: '@',
    name: '@'
},

this.parseInfo = JSON.parse(this.info);
this.name = ...

//and I then just use $ctrl.parseInfo in template...

One-time binding

An expression that starts with :: is considered a one-time expression. One-time expressions will stop recalculating once they are stable, which happens after the first digest if the expression result is a non-undefined value.

-- AngularJS Developer Guide - Expressions - One-Time Binding.

使用 one-time one-way 绑定来减少摘要周期开销:

 <my-component info="::$ctrl.infoObject"></my-component> 

JS

 app.component("myComponent", {
     bindings: {info: "<"},
     template: '<p>{{::$ctrl.info}}</p>',
 });