[转]Angular2: Cannot read property 'name' of undefined
本文转自:https://stackoverflow.com/questions/39755336/angular2-cannot-read-property-name-of-undefined
处理方式1:
The variable selectedHero is null in the template so you cannot bind selectedHero.name as is. You need to use the elvis operator for this case:
<input [ngModel]="selectedHero?.name" (ngModelChange)="selectedHero.name = $event" />
The separation of the [(ngModel)] in [ngModel] and (ngModelChange) is also needed because you can't assign to an expression that uses the elvis operator.
I also think you mean to use:
<h2>{{selectedHero?.name}} details!</h2>
instead of:
<h2>{{hero.name}} details!</h2>
处理方式2:
You just needed to read a little further and you would have been introduced to the *ngIf structural directive.
selectedHero.name doesn't exist yet because the user has yet to select a hero so it returns undefined.
<div *ngIf="selectedHero">
<h2>{{selectedHero.name}} details!</h2>
<div><label>id: </label>{{selectedHero.id}}</div>
<div>
<label>name: </label>
<input [(ngModel)]="selectedHero.name" placeholder="name"/>
</div>
</div>
The *ngIf directive keeps selectedHero off the DOM until it is selected and therefore becomes truthy.
This document helped me understand structural directives.
[转]Angular2: Cannot read property 'name' of undefined的更多相关文章
- angular2在模板中使用属性引发Cannot read property 'xxx' of undefined
angular在模板中使用属性引发Cannot read property 'xxx' of undefined 在使用ng2的过程中,发现模板中如下方式 <li *ngFor="le ...
- easyui Datagrid查询报错Uncaught TypeError:Cannot read property 'length' of undefined
1.问题描述 easyui中datagrid执行loadData方法出现如下异常:Cannot read property 'length' of undefined 2.一开始怀疑是js或者页面的问 ...
- vue.common.js?e881:433 TypeError: Cannot read property 'nodeName' of undefined
我觉得吧,是这么个原因,就是响应式要找这个node改它的内容,没找着,就报错了. 用computed监控vuex的state属性,绑定到页面上,如果这个属性改了,因为响应式,那么就要更改页面,如果页面 ...
- Uncaught TypeError: Cannot read property 'msie' of undefined
因为图方便,抄了别人写的一个jquerry插件,运行时“var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ...
- SharePoint 2013 Error - TypeError: Unable to get property 'replace' of undefined or null reference
错误信息 TypeError: Unable to get property ‘replace’ of undefined or null referenceTypeError: Unable to ...
- jquery错误: Cannot read property ‘msie’ of undefined
背景 Web application, 引用了jquery 1.10.2和fancybox 1.3.4 现象 访问页面遭遇Cannot read property ‘msie’ of undefine ...
- easyui使用时出现这个Uncaught TypeError: Cannot read property 'combo' of undefined
easyui使用时出现这个Uncaught TypeError: Cannot read property 'nodeName' of undefined 最后检查发现是必须给select一个id,光 ...
- [转载][jQuery] Cannot read property ‘msie’ of undefined错误的解决方法
参考 [jQuery] Cannot read property ‘msie’ of undefined错误的解决方法 ---------------------------------------- ...
- [jQuery] Cannot read property ‘msie’ of undefined错误的解决方法
最近把一个项目的jQuery升级到最新版,发现有些页面报错Cannot read property ‘msie’ of undefined.上jQuery网站上搜了一下,原因是$.browser这个a ...
随机推荐
- Elasticsearch下安装ik分词器
安装ik分词器(必须安装maven) 上传相应jar包 解压到相应目录 unzip elasticsearch-analysis-ik-master.zip(zip包) cp -r elasticse ...
- const和static readonly 区别
const的值是在编译期间确定的,因此只能在声明时通过常量表达式指定其值. 而static readonly是在运行时计算出其值的,所以还可以通过静态构造函数来赋值. static readonly ...
- vue报错TypeError: Cannot read property '$createElement' of undefined
报错截图: 这个错误就是路由上的component写成了components
- JS验证码邮件
js var time = 30; var canSend = true; function f5() { if (canSend) {//判断是否要ajax发送刷新验证码 验证码在后台刷新 //al ...
- 2.3.7synchronized代码块有volatile同步的功能
关键字synchronized可以使多个线程访问同一个资源具有同步性,而且他还具有将线程工作内存中的私有变量与公共内存中的变量同步的功能. package com.cky.thread; /** * ...
- java 项目的路径详情
title: 项目下的路径问题tags:grammar_cjkRuby: true--- 在javaee的项目中,存取文件,解析xml和properties文件,以及项目中的文件,都需要获取路径,常用 ...
- 数据结构(一): 键值对 Map
Map基本介绍 Map 也称为:映射表/关联数组,基本思想就是键值对的关联,可以用键来查找值. Java标准的类库包含了Map的几种基本的实现,包括:HashMap,TreeMap,LinkedHas ...
- 从app上传图片到php,再上传到java后端服务器的方法一条龙服务
在现在的网络开发中,上传图片类的需求实在是太普通不过了,但是对于怎么样做到上传图片,对于刚开始建立项目的时候,还是有点不知所措的.也许有幸,我们做的项目是之前已经有人写过类似的用例了,那么我们只需要依 ...
- WebMvcConfigurer
[传送门]:详解WebMvcConfigurer接口 1. 设置跨域规则 @Configuration public class CrossOriginConfig implements WebMvc ...
- HoloLens开发手记 - 入门学习阶段总结
伴随着数月的期待,终于拿到了预订的HoloLens开发者版本套件.随着VR/AR/MR技术的热潮,国内外均对它们的应用与盈利前景持有积极的预期,这也直接导致了国内外当前投资VR/AR/MR技术的热潮. ...