input绑定:
@H_502_2@要先在app.Module中导入FormModule,否则会Console会报错:Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input’
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [...
FormsModule,....
参考
<form class="form-inline">
<div class="form-group">
<label class="sr-only" for="inputNickName">NickName</label>
<div class="input-group">
<div class="input-group-addon">昵称:</div>
<input type="text" class="form-control" id="inputNickName" [(ngModel)]="nickName" placeholder="NickName">
<!--<input [(ngModel)]="nickName"> <input [value]="nickName" (input)="nickName = $event.target.value">-->
</div>
</div>
<button type="button" class="btn btn-primary" (click)="Login()">进入聊天室</button>
</form>
运行后显示正常,但F12 Console会报错:
If ngModel is used within a form tag,either the name attribute must be set or the form control must be defined as ‘standalone’ in ngModelOptions.
链接
在一个form标签中使用f ngModel,必须设置name属性,或者在ngModelOptions中必须将表单控件定义为“standalone”。[ngModelOptions]=”{standalone: true}”
<input type="text" class="form-control" id="inputNickName" [(ngModel)]="nickName" name="NickName placeholder="NickName">
<!--或-->
<input type="text" class="form-control" id="inputNickName" [(ngModel)]="nickName" [ngModelOptions]="{standalone: true}" placeholder="NickName">
Angular使用toastr,消息提示插件。
@H_502_2@'ngx-toastr': 'npm:_ngx-toastr@8.1.0@ngx-toastr/toastr.umd.js'
@H_502_2@
Angular版本升级
@H_502_2@httpService
@H_502_2@angular_cli取代systemjs
@H_502_2@systemjs的配置文件是system.config.js,angular_cli的配置文件是 .angular-cli.json,app的index要添加什么都在.angular-cli.json中配置了。angular-cli的web开发前后端的分离开发,刚用起来有点别扭,不知道怎么弄?查看 从MVC到前后端分离。
@H_502_2@
ng build
6. iis上新建网站,并浏览。
7. **如果是部署在现有IIS网站下的虚拟目录,必须删除index.html中的下面一段,否则js基本、图等资料会找不到。
<base href="/">
8. Hehe,眼睛一下就亮了,总算有头绪了。
—–====2018/1/11
重写以上
@H_502_2@@H_502_2@
npm uninstall -g angular-cli
npm cache clean (--forced)
npm install -g @angular/cli@latest
`ng -v
@H_502_2@
npm uninstall --save-dev angular-cli
npm install --save-dev @angular/cli@latest
npm install
按2 重新安装bootstrap,安装提示ootstrap@next有依赖没安装。
cnpm install ngx-bootstrap bootstrap@next --save
安装依赖。
cnpm install popper.jscnpm install jquery@1.9.1
ng serve 运行后总是不显示boostrap的样式,是因为用的cnpm安装的,使用cnpm安装的在.angular.cli.json 的Styles配置要带版本。参考。原因是:简单来说,npminstall 下的 node_modules 目录采用和 npm 官方 client 不一样的布局,主要是为了最大限度提高安装速度。参见1,2。
cnpm安装第三方插件 .angular-cli.json引用
Angular 5 +Angualr-CLI + bootstrap + toastr完成
文件
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { BsDropdownModule } from 'ngx-bootstrap/dropdown';
import { FormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ToastrModule } from 'ngx-toastr';
@NgModule({
declarations: [
AppComponent
],imports: [
BrowserModule,FormsModule,BsDropdownModule.forRoot(),BrowserAnimationsModule,// required animations module
ToastrModule.forRoot(),// ToastrModule added
],providers: [],bootstrap: [AppComponent]
})
export @H_97_404@class AppModule { }
app.component.ts
import { Component } from '@angular/core';
import { ToastrService } from 'ngx-toastr';
@Component({
selector: 'app-root',templateUrl: './app.component.html',styleUrls: ['./app.component.css']
})
export @H_97_404@class AppComponent {
title = 'app';
constructor(private toastr: ToastrService) { }
showSuccess() {
this.toastr.success('Hello world!','Toastr fun!');
}
}
app.component.html
<!--The content below is only a placeholder and can be replaced.-->
<div style="text-align:center">
<h1>
Welcome to {{ title }}!
</h1>
</div>
<button type="button" class="btn btn-dark" (click)="showSuccess()">
Button
</button>
<div class="btn-group" dropdown>
<button dropdownToggle type="button" class="btn btn-primary dropdown-toggle">
Button dropdown <span class="caret"></span>
</button>
<ul *dropdownMenu class="dropdown-menu" role="menu">
<li role="menuitem"><a class="dropdown-item" href="#">Action</a></li>
<li role="menuitem"><a class="dropdown-item" href="#">Another action</a></li>
<li role="menuitem"><a class="dropdown-item" href="#">Something else here</a></li>
<li class="divider dropdown-divider"></li>
<li role="menuitem"><a class="dropdown-item" href="#">Separated link</a>
</li>
</ul>
</div>
packages.json
{
"name": "my-cliapp","version": "0.0.0","license": "MIT","scripts": { "ng": "ng","start": "ng serve","build": "ng build --prod","test": "ng test","lint": "ng lint","e2e": "ng e2e" },"private": true,"dependencies": { "@angular/animations": "^5.2.0","@angular/common": "^5.1.0","@angular/compiler": "^5.1.0","@angular/core": "^5.1.0","@angular/forms": "^5.1.0","@angular/http": "^5.1.0","@angular/platform-browser": "^5.1.0","@angular/platform-browser-dynamic": "^5.1.0","@angular/router": "^5.1.0","bootstrap": "^4.0.0-beta.3","core-js": "^2.4.1","ngx-bootstrap": "^2.0.0-rc.0","ngx-toastr": "^8.1.0","rxjs": "^5.5.2","zone.js": "^0.8.19" },"devDependencies": { "@angular/cli": "1.6.4","@angular/compiler-cli": "^5.1.0","@angular/language-service": "^5.1.0","@types/jasmine": "~2.8.3","@types/jasminewd2": "~2.0.2","@types/node": "~6.0.60","codelyzer": "^4.0.1","jasmine-core": "~2.8.0","jasmine-spec-reporter": "~4.2.1","karma": "~2.0.0","karma-chrome-launcher": "~2.2.0","karma-cli": "~1.0.1","karma-coverage-istanbul-reporter": "^1.2.1","karma-jasmine": "~1.1.0","karma-jasmine-html-reporter": "^0.2.2","protractor": "~5.1.2","ts-node": "~3.2.0","tslint": "~5.9.1","typescript": "~2.5.3" } }