ngModel,但未在angular 2中提供模型名称 [英] ngModel without providing model name in angular 2

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

问题描述

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

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

解决方案

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

例如:您正在使用服务实例化页面上的所有对象.每当用户进入您的页面之一时,您都不想让该用户等待服务的响应,但是您想立即向他们显示页面的所有内容.服务响应将稍后实例化对象,然后才将其显示给用户.

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

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

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

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

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 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.

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 just gives you this possibility for free because whenever the object doesn't exist, it just won't show them to the user.

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天全站免登陆