您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Angular中什么是变更检测?什么情况下会引起变更检测?

2024/5/4 22:26:08发布38次查看
angular中什么是变更检测?下面本篇文章带大家了解一下变更检测,并介绍一下什么情况下会引起变更检测,希望对大家有所帮助!
什么是变更检测?简单来说,变更检测就是angular用来检测视图与模型之间绑定的值是否发生了改变,当检测到模型中的值发生改变时,则同步到视图上,反之,当检测到视图上的值发生改变时,则回调对应的绑定函数。【相关教程推荐:《angular教程》】
也就是,把模型的变化和视图保持一致的机制,这种机制,我们称为变更检测。
在angular里,开发者无需把精力放到具体的dom更新上,关注与业务就可以了,因为这部分工作angular帮我们做了。
如果不用angular的话,用原生的js开发,我们必须手动的去更新dom,先来看一个例子。
<html> <div id="datadiv"></div> <button id="btn">updatedata</button> <canvas id="canvas"></canvas> <script> let value = 'initialvalue'; // initial rendering detectchange(); function renderhtml() { document.getelementbyid('datadiv').innertext = value; } function detectchange() { const currentvalue = document.getelementbyid('datadiv').innertext; if (currentvalue !== value) { renderhtml(); } } // example 1: update data inside button click event handler document.getelementbyid('btn').addeventlistener('click', () => { // update value value = 'button update value'; // call detectchange manually detectchange(); }); // example 2: http request const xhr = new xmlhttprequest(); xhr.addeventlistener('load', function() { // get response from server value = this.responsetext; // call detectchange manually detectchange(); }); xhr.open('get', serverurl); xhr.send(); // example 3: settimeout settimeout(() => { // update value inside settimeout callback value = 'timeout update value'; // call detectchange manually detectchange(); }, 100); // example 4: promise.then promise.resolve('promise resolved a value').then(v => { // update value inside promise thencallback value = v; // call detectchange manually detectchange(); }, 100); // example 5: some other asynchronous apis document.getelementbyid('canvas').toblob(blob => { // update value when blob data is created from the canvas value = `value updated by canvas, size is ${blob.size}`; // call detectchange manually detectchange(); }); </script></html>
在上面的例子中,我们更新数据后,需要调用detectchange() 来检查数据是否已更改。如果数据已经更改,则渲染html以反应更新的数据。当然,在angular中,开发者无需关心这些步骤,只需要更新你的数据就可以了,dom会自动更新。这就是变更检测。
什么情况下会引起变更检测变更检测的关键在于如何最小粒度地检测到绑定的值是否发生了改变,那么在什么情况下会导致这些绑定的值发生变化呢?
结合日常开发,来看几种场景。
场景一
组件初始化:
当启动 angular 应用程序时,angular 会加载引导组件并触发 applicationref.tick() 来调用变更检测和视图渲染。
场景二
dom和bom事件:
dom 事件或bom事件侦听器可以更新 angular 组件中的数据,还可以触发变更检测,如下例所示。
@component({ selector: "counter", template: ` count:{{ count }} <br /> <button (click)="add()">add</button> `,})export class countercomponent { count = 0; constructor() {} add() { this.count = this.count + 1; }}
我们在视图上通过插值表达式绑定了counter中的count属性,当点击按钮时,改变了count属性的值,这时就导致了绑定的值发生了变化。
场景三
http数据请求:
@component({ selector: "todos", template: ` <li *ngfor="let item of todos">{{ item.titme }}</li> `, }) export class todoscomponent implements oninit { public todos: todoitem[] = []; constructor(private http: httpclient) {} ngoninit() { this.http.get<todoitem[]>("/api/todos").subscribe((todos: todoitem[]) => { this.todos = todos; }); } }
我们在todos这个组件里向服务端发送了一个ajax请求,当请求返回结果时,会改变视图中绑定的todos的值。
场景四
其他宏任务和微任务:
比如 settimeout() 或 setinterval()。你还可以在 settimeout() macrotask 的回调函数中更新数据。
@component({ selector: 'app-root', template: '<div>{{data}}</div>';})export class appcomponent implements oninit { data = 'initial value'; ngoninit() { settimeout(() => { // user does not need to trigger change detection manually this.data = 'value updated'; }); }}
实际开发中可能会在某一个函数里调用定时器去改变一个绑定的值。
再比如 promise.then() 。其他异步 api(比如 fetch)会返回 promise 对象,因此 then() 回调函数也可以更新数据。
@component({ selector: 'app-root', template: '<div>{{data}}</div>';})export class appcomponent implements oninit { data = 'initial value'; ngoninit() { promise.resolve(1).then(v => { // user does not need to trigger change detection manually this.data = v; }); }}
场景五
其他异步操作:
除了 addeventlistener(),settimeout() 和 promise.then() ,还有其他一些操作可以异步更新数据。比如 websocket.onmessage() 和 canvas.toblob() 。
不难发现,上述几种情况都有一个共同点,就是导致绑定值发生改变的事件都是 异步事件。只要发生了异步操作,angular就会认为有状态可能发生了变化,然后进行变更检测。
思考:还有哪些是异步事件啊?
这些包含了应用程序可能会在其中更改数据的最常见的场景。只要angular检测到数据可能已更改,就会进行变更检测,变更检测的结果是根据这些新数据dom被更新。angular 会以不同的方式检测变化。对于组件初始化,angular 调用显式变更检测。对于异步操作,angular 会使用 zone 在数据可能被修改的地方检测变化,并自动运行变更检测。
那如何订阅这些异步事件呢?请期待下一篇哦。
更多编程相关知识,请访问:编程教学!!
以上就是angular中什么是变更检测?什么情况下会引起变更检测?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product