ngModel 不提供 angular 2 中的模型名称 [英] ngModel without providing model name in angular 2

查看:21
本文介绍了ngModel 不提供 angular 2 中的模型名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <input #gb type="text" pInputText class="ui-widget ui-text" ngModel  
 (ngModelChange)="clearFilter($event)">

我没有在我的代码中为 ngModel 指令分配任何模型名称,但 Angular 2 接受这一点,而 AngularJS (1.x) 不接受.我们需要在哪种情况下使用这种 ngModel 指令而不提供模型名称?

I didn't assign any model name to ngModel directive in my code, but Angular 2 accepts this, while AngularJS (1.x) doesn't. Which scenario we need to use this kind of ngModel directive without providing model name?

推荐答案

Angular 2 接受它,因为该对象可以稍后设置.一种正确的编码方式是先检查是否对象存在,然后才显示对象是否存在.

Angular 2 accepts it because the object could be set later. A proper way of coding this would be to check if the object exists first, and only then show the object if it exists.

例如:您正在使用服务来实例化页面上的所有对象.每当用户访问您的页面时,您都不想让用户等待服务的响应,但您希望立即向他们显示页面的所有内容.服务响应稍后将实例化对象,然后才将它们显示给用户.

For example: You are using a service to instantiate all your objects on the page. Whenever a user goes to one of your pages you do not want to let the user wait for the response of the service, but you want to show them all the content of the page immediately. The service response will instantiate the objects later and only then show them to the users.

这可以通过例如 Angular 2 提供的 onInit 接口来完成.这确保您可以在 html 元素完全加载后调用服务并实例化对象.

This could be done for example by the onInit interface that Angular 2 provides. This makes sure that you can call on services and instantiate objects after the html elements are already fully loaded.

Angular2 只是免费为您提供这种可能性,因为只要对象不存在,它就不会向用户显示它们.

Angular2 just gives you this possibility for free because whenever the object doesn't exist, it just won't show them to the user.

angularJs (Angular1) 和 Angular(Angular2 或 Angular4) 之间的差异是巨大的,这只是众多例子中的一个.你可以这样看:这两个框架(AngularJS 和 Angular2)唯一的共同点是它们共享几个相同的字母.

The difference between angularJs (Angular1) and Angular(Angular2 or Angular4) are huge and this is just one of many examples. You could look at it this way: the only thing the two frameworks (AngularJS and Angular2) have in common is that they share a couple of the same letters.

这篇关于ngModel 不提供 angular 2 中的模型名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆