我们已经完成了一个angular2项目设置和内部创建了一个模块(my-module),并在该模块内部使用以下cmd命令创建了一个组件(my-new-component):
ng new angular2test
cd angular2test
ng g module my-module
ng generate component my-new-component
Run Code Online (Sandbox Code Playgroud)
在创建设置和所有组件之后,我们ng test
从angular2test文件夹中的cmd 运行命令.
下面的文件是我的my-new-component.component.ts文件:
import { Component, OnInit } from '@angular/core';
import { Router, Routes, RouterModule } from '@angular/router';
import { DummyService } from '../services/dummy.service';
@Component({
selector: 'app-my-new-component',
templateUrl: './my-new-component.component.html',
styleUrls: ['./my-new-component.component.css']
})
export class MyNewComponentComponent implements OnInit {
constructor(private router : Router, private dummyService:DummyService) { }
ngOnInit() {
}
redirect() : void{
//this.router.navigate(['/my-module/my-new-component-1'])
}
}
Run Code Online (Sandbox Code Playgroud)
下面的文件是我的my-new-component.component.spec.ts文件:
/* tslint:disable:no-unused-variable */
import { …
Run Code Online (Sandbox Code Playgroud) unit-testing karma-jasmine angular2-routing angular-cli angular
我创建了一个angular-cli
包含AppComponent的项目,如下所示:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app works!';
}
Run Code Online (Sandbox Code Playgroud)
和app.component.html一样
<h1>
Good Morning, {{title}}
</h1>
Run Code Online (Sandbox Code Playgroud)
因此,当我使用ng build
它生成它时会生成一些像这样的文件./dist/main.bundle.js,其中包含一些代码,如下所示 -
/* 586 */
/***/ function(module, exports) {
module.exports = "<h1>\n Good Morning, {{title}}\n</h1>\n"
/***/ },
/* 587 */
Run Code Online (Sandbox Code Playgroud)
这意味着,在构建时,编译器/ bundle-er正在读取html文件并将它们连接到生成的js文件中.
但在我的情况下,html也是动态的,内容驱动的是服务器端.可以说,我的模板文件不是html,而是app.component.jsp,并且完全驻留在一些不同的服务器或文件夹上.
此JSP文件有时会返回<h1>Good Morning, {{title}}</h1>
,有时还<h1>Good Afternoon, {{title}}</h1>
取决于当前的服务器时间.
如何实现这一功能?
我的理解是,我需要定义某种加载器函数说: loadDynamicTemplate(template_url) …
我附上了我的angular2代码片的plunker.我想从我的JSON打印一个字段但是无法打印,因为最初我的Object是null并且它是通过Promise填充的.
这是我的组件文件
import {Component, NgModule, OnInit} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
class MyData {
xyz : MySubData;
}
class MySubData {
name : string;
}
@Component({
selector: 'my-app',
template: `
<div>
<h2>Hello {{name}}</h2>
{{abc.xyz.name}}
</div>
`,
})
export class App implements OnInit {
abc : MyData = null;
constructor() {
this.name = 'Angular2'
}
ngOnInit() {
setTimeout(() => {
this.abc = new MyData();
this.abc.xyz = new MySubData();
this.abc.xyz.name = "Binita";
}, 2000);
}
}
@NgModule({
imports: [ BrowserModule ], …
Run Code Online (Sandbox Code Playgroud) 据我所知,我们使用try catch
如下:
try {
//Some code that may generate exception
}
catch(Exception ex) {
}
//handle exception
finally {
//close any open resources etc.
}
Run Code Online (Sandbox Code Playgroud)
但在我发现的代码中
try(
ByteArrayOutputStream byteArrayStreamResponse = new ByteArrayOutputStream();
HSLFSlideShow pptSlideShow = new HSLFSlideShow(
new HSLFSlideShowImpl(
Thread.currentThread().getContextClassLoader()
.getResourceAsStream(Constants.PPT_TEMPLATE_FILE_NAME)
));
){
}
catch (Exception ex) {
//handel exception
}
finally {
//close any open resource
}
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么这个括号()
在尝试之后.
它的用途是什么?它是Java 1.7中的新功能吗?我可以在那里写什么样的语法?
还请参考一些API文档.
与此问题类似
我正在运行以下命令来lint我的angular2 typeScript代码.
ng lint
Run Code Online (Sandbox Code Playgroud)
它很好地证明了所有的掉毛错误.
但我希望我的供应商文件夹(假设"src/app/quote/services/generated/**/*")不应该包含在内衬时.
我知道这可以用tslint命令完成,如下所示(参考这里)
tslint \"src/**/*.ts\" -e \"**/__test__/**\"
Run Code Online (Sandbox Code Playgroud)
但在角度cli中,参数是什么?如何从tslint中排除一些文件?
注意:我的Angular Cli版本是:@ angular/cli:1.0.0-rc.0
我有一个数字让我说9.但我需要它作为字符串"09".
我知道我可以写自己的逻辑.但我正在寻找一个隐含的util函数,它可以填充它.
我在TypeScript中使用angular2.寻找像这样.
就像java一样
String.format("%010d", Integer.parseInt(mystring));
Run Code Online (Sandbox Code Playgroud) 这是插件. https://plnkr.co/edit/tsNlmRth4mRzz0svGWLK?p=preview
我在其中创建了两个模块,每个模块包含两个组件和一个服务.我希望服务应该是模块级别的单例(保存状态).
如果单击"模块1页面1"和"模块2页面2",它将显示两个不同的随机数.因为我在构造函数中生成此数字.因此,每次页面更改时都会创建服务.但是module2的表现非常完美.
注意:Mode1Module正在使用loadChildren
Mode2Modulechildren
我发现这个相同类型的问题早先已根据此angular2 rc5路由器服务单例修复
但我认为它仍然存在.所以请帮我解决这个问题.
好吧,我使用Angular 2创建了网站.当我运行"ng-serve"命令并尝试在Google Chrome和IE11中使用" http:// localhost:4200 " 测试我的网站时,它运行正常,但是,如果我构建项目使用"ng-build --prod",在IIS上托管它.网站仍在Chrome上运行,但IE11显示以下错误:
我用谷歌搜索它,发现它可能是我的标识符键中的保留字:值对.所以我在钥匙上添加了Apostrophes(').例如,我有以下对象:
user:UserViewModel={
Username:"",
Age:0
};
and changed this to:
user:UserViewModel={
'Username':"",
'Age':0
};
Run Code Online (Sandbox Code Playgroud)
还删除了key:value对中的最后一个逗号,并在polyfills.ts中导入了core.js
我想创建一个类似于AngularJS实现"email"的方式的自定义输入类型.
<input type="email" ng-model="user.email" />
Run Code Online (Sandbox Code Playgroud)
我想创建的是这样的输入类型:
<input type="path" ng-model="page.path" />
Run Code Online (Sandbox Code Playgroud)
关于如何实现这一点的任何想法?到目前为止,我只能弄清楚如何实现自定义指令,其中'path'是标记,属性或类的名称.
例如,我可以让它工作,但它与其他表单字段不一致,我真的希望它们看起来一样.
<input type="text" ng-model="page.path" path />
Run Code Online (Sandbox Code Playgroud)
app.directive('path', function() {
return {
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) { ... }
};
});
Run Code Online (Sandbox Code Playgroud) angular ×7
angular-cli ×4
typescript ×3
aem ×1
angularjs ×1
c# ×1
datetime ×1
iis ×1
java ×1
javascript ×1
padding ×1
parentheses ×1
string ×1
try-catch ×1
tslint ×1
unit-testing ×1