Angularjs指令中的ngModel

Home » JavaScript » 前端开发 » Angularjs指令中的ngModel

文章归档

文件夹

之前用的时候忘记总结了,这里集中总结一下这个问题。

使用require:‘ngModel’ 这个选项来增强我们对指令的处理,

这样就可以作为link选项的第四个参数,
link: function ($scope,$element,$attrs,$ngModel){
//其他逻辑代码
}

这里面的$ngModel便是代码中ng-model指令传递过来的对象,包含以下这些方法和属性:

  • $viewValue:视图值,即显示在视图(页面)的实际值(就是上面例子中input输入框的值)
  • $modelValue:模型值,即赋给ng-model的值(与控制器绑定的值)
    两者不一定相等,因为$viewValue同步到$modelValue要经过一系列的操作(经过三个管道)。
  • $parsers:一个执行它里面每一个元素(每一个元素都是一个函数)的数组,
    主要是用来做验证和转换值的过程,ngModel从DOM读取的值会被传入到其中的函数它会依次执行每一个函数,把每一个函数执行的结果传个下一个函数,而最后一个函数执行的值将会传到model中,

    我们可以将函数push进去,那样它就会执行。

  • $formatters:也是一个执行它里面每一个元素(每一个元素都是一个函数)的数组,主要用来对值进行格式化和转换,以便在绑定了这个值的控件中显示。当数据的模型值发生变化的时候,里面的函数会被一一执行,同样我们就可以将函数push进去,让它执行
  • $viewChangeListeners:也是一个由函数组成的数组当视图的值发生变化的时候里面的函数会被一一调用,实现跟$watch类似的功能。
  • $render:函数负责将模型值同步到视图上, 如果模型值被改变,需要同步视图的值。
  • $setViewValue:用于设置视图值(上面的例子就是将input的value值赋值给$viewValue)
  • $error:一个包含所有error的对象
  • $setPristine:设置为原始状态,会添加ng-pristine的class类名,移除ng-dirty的class类名
  • $setValidity:设置错误的标志为一个函数,接受两个参数,第一个参数为错误标志的名字,是字符串类型,将会被设置成$error的属性第二个参数为布尔值,为这个错误标志的值。

     

16 Responses to “Angularjs指令中的ngModel”
  • jordan 6

    My husband and i felt absolutely satisfied that Peter managed to complete his survey from the precious recommendations he acquired using your site. It is now and again perplexing to just continually be giving out procedures which often most people may have been selling. And we all already know we have the website owner to give thanks to because of that. The specific illustrations you’ve made, the simple blog navigation, the relationships you help promote – it’s most remarkable, and it is helping our son in addition to the family do think that subject matter is excellent, and that’s especially pressing. Thank you for everything!

    回复
  • nfl store

    I together with my guys were actually studying the nice things located on your site and so quickly came up with an awful suspicion I never thanked the site owner for those techniques. Those young boys are actually so happy to learn them and now have in fact been tapping into those things. Many thanks for genuinely indeed kind and also for deciding upon such fabulous resources millions of individuals are really desirous to be informed on. My sincere apologies for not saying thanks to you sooner.

    回复
  • kobe byrant shoes

    I would like to express some thanks to this writer just for rescuing me from this particular challenge. Just after researching throughout the world wide web and seeing suggestions which are not helpful, I was thinking my entire life was done. Being alive devoid of the strategies to the problems you’ve fixed as a result of the short article is a serious case, and the ones which could have negatively affected my entire career if I had not come across the blog. Your actual mastery and kindness in dealing with a lot of things was helpful. I am not sure what I would’ve done if I had not encountered such a thing like this. I can also at this point relish my future. Thanks very much for this high quality and amazing help. I won’t hesitate to propose the blog to any individual who needs tips on this situation.

    回复
  • adidas ultra boost 3.0

    I simply desired to say thanks once more. I’m not certain the things I would’ve handled without the entire hints discussed by you over my area of interest. It was before an absolute troublesome situation in my opinion, however , considering this specialised technique you managed the issue forced me to cry over contentment. Extremely happier for this work and in addition expect you realize what an amazing job you’re getting into instructing the rest with the aid of your webpage. I’m certain you haven’t met all of us.

    回复
  • air jordan shoes

    I simply desired to say thanks again. I do not know what I could possibly have done without these basics revealed by you over my subject. This was an absolute terrifying issue in my opinion, but noticing a well-written form you resolved the issue made me to leap over gladness. Now i am thankful for this help and then have high hopes you find out what an amazing job that you are undertaking educating other individuals using a blog. I am sure you have never got to know all of us.

    回复
  • michael kors factory outlet

    I not to mention my pals were actually viewing the best pointers found on your web page and so at once came up with an awful suspicion I never thanked the web blog owner for those strategies. The young boys were definitely as a consequence passionate to see them and have in effect in fact been having fun with those things. We appreciate you truly being quite considerate and then for choosing variety of decent areas most people are really eager to understand about. My honest regret for not expressing appreciation to sooner.

    回复
  • yeezys

    I would like to get across my admiration for your generosity in support of people who need guidance on the subject matter. Your real dedication to passing the solution all over had become unbelievably beneficial and have in every case encouraged professionals just like me to get to their targets. This important publication entails so much to me and further more to my peers. Warm regards; from all of us.

    回复
  • mlb jerseys

    My spouse and i got really thrilled that Louis managed to conclude his studies while using the ideas he obtained when using the web site. It is now and again perplexing just to always be giving out thoughts which often some other people might have been making money from. And we also consider we have got the website owner to give thanks to for that. The entire explanations you’ve made, the simple web site navigation, the relationships you assist to create – it’s everything terrific, and it’s aiding our son and us know that the content is entertaining, which is especially essential. Thanks for all the pieces!

    回复
  • longchamp handbags

    I am glad for writing to make you be aware of what a excellent discovery my girl went through using your web page. She came to understand so many things, most notably what it is like to have a very effective giving mindset to make others without hassle gain knowledge of selected impossible topics. You undoubtedly did more than visitors’ expectations. Thanks for offering such valuable, safe, educational and even easy thoughts on the topic to Janet.

    回复
  • nike max

    Thanks a lot for giving everyone an extraordinarily special possiblity to read critical reviews from this web site. It really is very pleasing and as well , stuffed with a good time for me personally and my office peers to search your website at least three times per week to learn the latest guides you will have. And of course, I’m certainly amazed with your astonishing creative concepts served by you. Some 1 tips in this article are honestly the best I have ever had.

    回复
  • adidas nmd

    I enjoy you because of every one of your hard work on this site. Gloria loves carrying out investigation and it’s easy to understand why. A lot of people know all concerning the lively ways you deliver useful items through the web site and as well as welcome response from other people on that idea while our child is always discovering a great deal. Take pleasure in the rest of the new year. You have been conducting a useful job.

    回复
  • bape hoodie

    I just wanted to construct a simple comment to thank you for the remarkable strategies you are showing on this website. My extended internet research has at the end of the day been rewarded with really good knowledge to write about with my family members. I ‘d say that we site visitors are truly endowed to be in a fantastic site with very many perfect individuals with great guidelines. I feel very much privileged to have used the weblog and look forward to plenty of more exciting moments reading here. Thanks once again for everything.

    回复
  • cheap jordans

    My spouse and i felt peaceful Louis managed to round up his survey through your ideas he discovered while using the web page. It’s not at all simplistic just to choose to be giving freely facts that many many others have been selling. And now we discover we’ve got the writer to be grateful to for this. All of the illustrations you made, the straightforward blog navigation, the friendships you will aid to engender – it’s got many great, and it is assisting our son and us understand that concept is thrilling, which is certainly quite pressing. Thank you for the whole thing!

    回复
  • Women Mid Star Sneakers

    I actually wanted to make a brief remark in order to appreciate you for some of the superb hints you are posting on this website. My incredibly long internet search has now been recognized with sensible insight to talk about with my partners. I would state that that many of us site visitors actually are definitely blessed to live in a wonderful place with so many perfect professionals with very helpful points. I feel pretty lucky to have used the web site and look forward to tons of more brilliant minutes reading here. Thanks a lot again for all the details.

    回复
  • golden goose sneakers

    I am also commenting to let you understand what a excellent experience our child developed browsing the blog. She noticed a good number of pieces, which included how it is like to possess a wonderful coaching mindset to make most people very easily fully grasp specific advanced issues. You truly exceeded readers’ expectations. Thanks for presenting those essential, healthy, explanatory and also cool guidance on this topic to Janet.

    回复
  • michael kors outlet

    I wanted to draft you that tiny remark just to say thank you again regarding the pretty knowledge you’ve featured in this article. It is simply incredibly generous with you to make unhampered all most of us would’ve sold as an e-book in making some cash for their own end, even more so now that you could possibly have done it if you considered necessary. These concepts likewise worked like the great way to be sure that other people online have the identical interest just like my personal own to find out very much more on the subject of this issue. I am certain there are millions of more pleasurable sessions in the future for those who read your site.

    回复

发表评论

Your email address will not be published.