Angular之生命周期钩子、组件交互(父组件向子组件传值)

1.新建angular项目
项目目录:


image.png

2.使用知识简介
这里主要是父组件想子组件传值,以及使用到生命周期。
ngOnChanges ngOnInit

3.基础知识
在angular项目中,主要是在ts文件中进行声明,然后html中进行布局。

4.index.html
在index.html入口文件中引入bootstrap,然后把初始组件搞进去

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Angular</title>
<base href="/">

<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"  
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"     
crossorigin="anonymous">
<!-- 可选的 Bootstrap 主题文件(一般不用引入) -->
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"   
integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp"   
crossorigin="anonymous">
</head>
<body>
  <div class="container">
  <app-root></app-root>
  </div>
 </body>
</html>

index.html通过app.component.ts文件中的组件别名 <app-root></app-root>找到父组件app。

随后在app.component.ts文件中声明父组件要使用到的一些变量

  import { Component } from '@angular/core';

 @Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.css']
 })
 export class AppComponent {

 _degress='';
tab_items = [];
tab_index='';
constructor(){
console.log('app-component-constructor!!!!');
this.tab_items=['全部','大专' ,'本科', '硕士', '博士']
}
select_tab(i){
this.tab_index = i;
this._degress=this.tab_items[this.tab_index];
 }
ngOnInit() {
console.log('app-component-ngoninit!!!!');
this._degress = this.tab_items[this.tab_index];
}
}

然后在app.component.html中传值给子组件:

<div class="row">
<div class="col-md-12">
  <ul class="nav nav-tabs">
    <li role="presentation" *ngFor="let tab of tab_items index as i" [class.active]="tab_index==i"
        (click)="select_tab(i)">
      <a href="#" [style.background]="tab_index==i?'red':''" >{{i}}{{tab}}</a>
    </li>
  </ul>
  <span>{{_degress}}</span>
</div>
<div class="col-md-12">
  <!--1. 父组件传值给子组件-->
  <app-position [degree_now]="_degress"></app-position>
</div>
</div>

子组件中的ts文件中使用@Input() 来声明变量,然后在在组建ts文件中封装json,这里没有后台直接用的一个json变量。再通过filter进行过滤查找想要的数据

 import { Component, OnInit,Input } from      
'@angular/core';

 @Component({
 selector: 'app-position',
 templateUrl: './position.component.html',
 styleUrls: ['./position.component.css']
 })
 export class PositionComponent implements OnInit {
 @Input() degree_now='';
position=[
{
  "name": "销售经理",
  "salary": "10k-15k ",
  "education": "大专",
  "years_working": "经验3-5年 ",
  "job_nature": "全职",
  "attraction": "与优秀的人一起共事 无限发展空间",
  "tags": "企业服务高级 后端开发 分布式 J2EE SOA",
  "city": "上海",
  "pubtime":"2017-05-24 08:22:00",
  "company": {
    "name": "高富帅人才网",
    "logo": "images/logo.png",
    "stage": "成熟型(D轮及以上) "
  }
},
{
  "name": "Java开发工程师 ",
  "salary": "15k-25k",
  "education": "本科及以上",
  "years_working": "经验5-10年",
  "job_nature": "兼职",
  "attraction": "技术挑战、工作居住证、美味果盘、运动健身",
  "tags": "本地生活 后端开发 平台/后台",
  "city": "杭州",
  "pubtime":"2017-06-19 12:32:00",
  "company": {
    "name": "饿了么",
    "logo": "images/logo.png",
    "stage": "移动互联网/成熟型(D轮及以上) "
  }
},
{
  "name": "客户经理 ",
  "salary": "5k-8k",
  "education": "本科及以上",
  "years_working": "经验1-3年",
  "job_nature": "全职",
  "attraction": "爱心两餐、水果下午茶、节日礼物、绩效奖金",
  "tags": "企业服务 顾问",
  "city": "上海",
  "pubtime":"2017-03-21 14:20:00",
  "company": {
    "name": "泛微",
    "logo": "images/logo.png",
    "stage": "移动互联网,企业服务 / 上市公司 "
  }
},
{
  "name": "销售专员 ",
  "salary": "3k-5k",
  "education": "大专及以上",
  "years_working": "经验1-3年",
  "job_nature": "兼职",
  "attraction": "节日礼物,年底双薪,绩效奖金,管吃喝",
  "tags": "企业服务 销售 专员",
  "city": "苏州",
  "pubtime":"2017-04-12 18:32:00",
  "company": {
    "name": "恋家网",
    "logo": "images/logo.png",
    "stage": "成熟型(D轮及以上) "
  }
},
{
  "name": "高级Java工程师 ",
  "salary": "15k-30k ",
  "education": "本科及以上",
  "years_working": "经验3-5年",
  "job_nature": " 全职",
  "attraction": "弹性工作制,股票期权,年终奖",
  "tags": "java 架构师 SSHA",
  "city": "背景",
  "pubtime":"2017-06-24 02:12:00",
  "company": {
    "name": "创意网",
    "logo": "images/logo.png",
    "stage": "上市公司"
  }
},
{
  "name": "新媒体运营专员",
  "salary": "4k-6k",
  "education": "本科及以上",
  "years_working": "经验1-3年",
  "job_nature": "全职",
  "attraction": "团队活动,节日福利,带薪年假,股票期权",
  "tags": "汽车 新媒体 自媒体 微信开发 专员\r\n",
  "city": "南京",
  "pubtime":"2017-06-23 08:22:00",
  "company": {
    "name": "网约车",
    "logo": "images/logo.png",
    "stage": "成熟型(C轮) "
  }
},
{
  "name": "数据挖掘工程师",
  "salary": "12k-24k ",
  "education": "本科及以上 ",
  "years_working": "经验1-3年",
  "job_nature": "全职",
  "attraction": "五险一金、带薪年假、股票期权、优秀的团队",
  "tags": "汽车 数据 建模 数据挖掘",
  "city": "苏州",
  "pubtime":"2017-06-23 10:12:00",
  "company": {
    "name": "网约车",
    "logo": "images/logo.png",
    "stage": "成熟型(C轮) "
  }
},
{
  "name": "数据建模 ",
  "salary": "16k-32k ",
  "education": " 本科及以上",
  "years_working": "经验1-3年",
  "job_nature": " 全职",
  "attraction": "节日福利,发展空间,晋升空间,股票期权",
  "tags": "数据分析 金融 建模 算法 matlab",
  "city": "苏州",
  "pubtime":"2017-06-23 21:12:00",
  "company": {
    "name": "网约车",
    "logo": "images/logo.png",
    "stage": "成熟型(C轮) "
  }
},
{
  "name": "数据挖掘-风控方向",
  "salary": "15k-30k",
  "education": "本科及以上",
  "years_working": "经验3-5年 ",
  "job_nature": " 全职",
  "attraction": "节日福利,发展空间,晋升空间,股票期权",
  "tags": "专家 资深 建模 汽车 数据挖掘 大数据",
  "city": "苏州",
  "pubtime":"2017-06-24 08:28:00",
  "company": {
    "name": "网约车",
    "logo": "images/logo.png",
    "stage": "成熟型(C轮) "
  }
},
{
  "name": "测试工程师 ",
  "salary": "8k-15k",
  "education": "博士",
  "years_working": "经验1-3年",
  "job_nature": " 全职",
  "attraction": "弹性工作制,股票期权,年终奖",
  "tags": "测试 java",
  "city": "上海",
  "pubtime":"2017-06-24 05:12:00",
  "company": {
    "name": "创意网",
    "logo": "images/logo.png",
    "stage": "上市公司"
  }
},
{
  "name": "前端开发",
  "salary": "15k-30k",
  "education": "硕士",
  "years_working": "经验3-5年",
  "job_nature": " 全职",
  "attraction": "弹性工作制,股票期权",
  "tags": "前端 HTML5",
  "city": "无锡",
  "pubtime":"2017-06-24 09:00:00",
  "company": {
    "name": "创意网",
    "logo": "images/logo.png",
    "stage": "上市公司"
  }
},
{
  "name": "HTML5",
  "salary": "16k-20k",
  "education": "本科及以上",
  "years_working": "经验3-5年",
  "job_nature": " 全职",
  "attraction": "弹性工作 一群文艺小骚年们等着你的加入",
  "tags": "教育 音乐 直播 B2C web php",
  "city": "北京",
  "pubtime":"2017-06-24 9:12:00",
  "company": {
    "name": "创意网",
    "logo": "images/logo.png",
    "stage": "上市公司"
  }
}
];
my_position='';
select_position=[];
constructor(){

}

ngOnChanges(){
if(this.degree_now == '全部'){
  this.select_position = this.position;
}else{
  let degree = this.degree_now;
  this.select_position=this.position.filter(function (pos) {
    return  ((pos.education).indexOf(degree)!=-1);
  });
}
}
ngOnInit() {
 this.select_position = this.position;
 }

}

最后在子组件HTML页面进行数据渲染就可以了:

<table class="table">
<div>
<span>{{my_name}}</span>
</div>
<tr>
<th>序号</th>
<th>职位</th>
<th>工资</th>
<th>学历</th>
</tr>
<tr *ngFor="let info of select_position index as i"   
(click)="my_name=info?.name" >
<td>{{i}}</td>
<td>{{info?.name}}</td>
<td>{{info?.salary}}</td>
<td>{{info?.education}}</td>
</tr>
</table>
image.png

!!需要注意的是父组件向子组件传值会在子组件声明中使用到@Input()

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,651评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,468评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,931评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,218评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,234评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,198评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,084评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,926评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,341评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,563评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,731评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,430评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,036评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,676评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,829评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,743评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,629评论 2 354

推荐阅读更多精彩内容