Angular.js中使用$watch监听模型变化

Home » 前端开发 » Angular.js中使用$watch监听模型变化

文章归档

文件夹

先说基本的

$watch简单使用

$watch是一个scope函数,用于监听模型变化,当你的模型部分发生变化时它会通知你。

 

$watch(watchExpression, listener, objectEquality);

每个参数的说明如下:

  1. watchExpression:监听的对象,它可以是一个angular表达式如’name’,或函数如function(){return $scope.name}。
  2. listener:当watchExpression变化时会被调用的函数或者表达式,它接收3个参数:newValue(新值), oldValue(旧值), scope(作用域的引用)
  3. objectEquality:是否深度监听,如果设置为true,它告诉Angular检查所监控的对象中每一个属性的变化. 如果你希望监控数组的个别元素或者对象的属性而不是一个普通的值, 那么你应该使用它。

简单的实现看书好了,我这里重点要说的是监听多个值。

$watch监听多个值

 

从第一个参数的说明里可以看出,可以用这样的函数做监听对象function(){return $scope.name}。

就是函数返回的值改变了,我们就认为值变了。

 

那么我们用这个方法监听多个值,如下:
$scope.$watch(function () {
return JSON.stringify([$scope.name, $scope.value])
}, function (newVal,oldVal) {
//...
});

以上面的方式就可以同时监听该作用域($scope)下的name和value两个值了。

 

关于$watchCollection

该方法允许我们为对象的属性或者数组的元素设置浅(一层)监控。

14 Responses to “Angular.js中使用$watch监听模型变化”
  • pandora bracelet

    I and my buddies appeared to be taking note of the best things from the blog while quickly developed a horrible suspicion I never expressed respect to the site owner for those tips. My young boys happened to be for that reason happy to see them and have in effect honestly been using these things. Appreciation for indeed being really considerate as well as for settling on some smart resources most people are really eager to be informed on. My personal honest regret for not expressing gratitude to you sooner.

    回复
  • nike basketball shoes

    I needed to put you the little observation to be able to say thanks over again just for the marvelous secrets you have shown in this article. It’s certainly extremely generous of you to grant openly what most of us might have made available for an electronic book in making some profit on their own, particularly now that you could have done it if you ever decided. These inspiring ideas additionally worked to be the good way to know that other people online have the same interest like my own to figure out much more with reference to this issue. I believe there are lots of more fun instances ahead for individuals who scan your site.

    回复
  • adidas ultra boost 3.0

    I needed to post you that very little observation to be able to give many thanks over again about the stunning tactics you have contributed on this site. This is seriously open-handed of people like you to make extensively all that a number of people would have sold for an electronic book to generate some money for themselves, principally seeing that you could possibly have tried it in the event you wanted. The guidelines additionally worked like the great way to fully grasp some people have the identical keenness like my own to figure out a whole lot more when it comes to this matter. I am sure there are many more enjoyable times ahead for individuals that browse through your blog post.

    回复
  • links of london sale

    I intended to put you one little bit of word to be able to thank you very much again for those great concepts you’ve shared here. It was really shockingly open-handed of people like you in giving unhampered all that a number of us would have distributed as an e-book to help make some bucks for their own end, precisely given that you could have tried it in case you considered necessary. Those secrets also served as the great way to be certain that other people online have the same desire much like my very own to understand a good deal more related to this problem. I’m sure there are many more pleasurable occasions in the future for many who check out your site.

    回复
  • air jordans

    I am just commenting to make you be aware of of the impressive discovery my daughter experienced viewing your site. She mastered many things, including what it’s like to have an amazing coaching heart to let many people completely know just exactly a variety of very confusing topics. You undoubtedly did more than our own desires. Thank you for delivering such precious, trustworthy, educational and as well as unique tips about this topic to Julie.

    回复
  • hermes bags

    My husband and i felt now fulfilled Peter managed to finish off his homework using the ideas he came across out of the site. It is now and again perplexing just to always be giving for free tips and tricks which often people have been selling. And now we already know we need you to thank for that. The most important explanations you have made, the easy web site menu, the relationships you can help to promote – it’s got everything astonishing, and it’s really facilitating our son in addition to us know that that theme is fun, which is very mandatory. Thank you for everything!

    回复
  • hogan outlet online

    My wife and i have been quite thrilled that John managed to complete his survey from the ideas he got in your blog. It’s not at all simplistic to simply possibly be making a gift of helpful tips others may have been making money from. And we already know we need the website owner to appreciate for that. All of the illustrations you’ve made, the straightforward web site navigation, the relationships you will help foster – it’s got many sensational, and it is assisting our son and us recognize that this matter is enjoyable, which is tremendously vital. Thank you for all the pieces!

    回复
  • gucci belts

    My spouse and i have been fulfilled when Raymond could finish up his research because of the ideas he came across through the site. It is now and again perplexing just to possibly be freely giving facts which many people may have been making money from. And we also consider we have got the writer to thank because of that. The type of illustrations you’ve made, the simple blog menu, the friendships your site assist to instill – it’s got most unbelievable, and it is facilitating our son and us imagine that that issue is satisfying, and that is exceptionally fundamental. Thanks for the whole lot!

    回复
  • zx flux

    I want to show some appreciation to you just for bailing me out of such a setting. Just after researching through the the net and coming across ideas which were not productive, I assumed my life was well over. Being alive devoid of the solutions to the problems you have solved by means of your entire article content is a crucial case, as well as the kind that could have badly affected my entire career if I had not noticed your site. Your personal knowledge and kindness in playing with every part was very useful. I’m not sure what I would’ve done if I hadn’t discovered such a subject like this. I am able to at this time look forward to my future. Thanks for your time so much for this impressive and result oriented help. I won’t be reluctant to recommend the website to any person who should get assistance about this topic.

    回复
  • nike cortez men

    I wish to express my passion for your kindness supporting persons who must have help with your niche. Your special commitment to getting the message all-around turned out to be remarkably insightful and has all the time permitted associates just like me to attain their objectives. This informative key points can mean much a person like me and additionally to my peers. Thank you; from all of us.

    回复
  • air max 270

    I simply wanted to post a simple remark to say thanks to you for these superb strategies you are giving out at this site. My time-consuming internet research has at the end of the day been rewarded with pleasant suggestions to go over with my guests. I ‘d say that most of us readers actually are undeniably lucky to live in a really good community with very many special professionals with very helpful secrets. I feel very lucky to have discovered your web page and look forward to tons of more amazing times reading here. Thanks once again for everything.

    回复
  • moncler outlet

    My wife and i ended up being so delighted when Ervin could finish off his investigations through the entire ideas he had from your weblog. It’s not at all simplistic to just find yourself offering methods that many other folks have been selling. And now we understand we now have the website owner to thank for that. All of the illustrations you have made, the straightforward blog navigation, the relationships you can give support to promote – it’s got everything astonishing, and it’s aiding our son in addition to the family recognize that the concept is interesting, which is certainly tremendously mandatory. Thanks for all!

    回复
  • golden goose sneakers

    I precisely needed to thank you very much yet again. I’m not certain what I could possibly have accomplished without these concepts discussed by you concerning this industry. It seemed to be a alarming issue in my view, but taking note of the professional tactic you handled the issue made me to cry for delight. I’m just happy for your advice and even have high hopes you are aware of an amazing job that you are accomplishing educating most people with the aid of your web blog. More than likely you haven’t come across any of us.

    回复
  • lebron 14

    My husband and i ended up being now fulfilled John could round up his survey through the entire precious recommendations he obtained from your own web site. It’s not at all simplistic to simply possibly be offering facts which usually most people could have been making money from. And we all see we’ve got the writer to give thanks to for that. The explanations you have made, the easy blog navigation, the friendships you can help to foster – it is many fantastic, and it’s facilitating our son in addition to our family recognize that the situation is entertaining, and that’s incredibly vital. Thanks for the whole lot!

    回复

发表评论

Your email address will not be published.