假设我有一个Angular2组件
//home.component.ts
import { Component } from 'angular2/core';
@Component({
selector: "home",
templateUrl: "app/components/templates/home.component.html",
styleUrls: ["app/components/styles/home.component.css"]
})
export class HomeComponent {
public width: Number;
public height: Number;
}
Run Code Online (Sandbox Code Playgroud)
此组件的模板html文件
//home.component.html
<div class="home-component">Some stuff in this div</div>
Run Code Online (Sandbox Code Playgroud)
最后是这个组件的css文件
//home.component.css
.home-component{
background-color: red;
width: 50px;
height: 50px;
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,该类中有2个属性,width和height.我希望宽度和高度的css样式与width和height属性的值匹配,当属性更新时,div更新的宽度和高度.完成此任务的正确方法是什么?
使用Visual Studio作为我的IDE处理项目.它有一个用C#编写的API组件,以及一个使用TypeScript的Web服务器组件.
我正在使用webpack来处理typescript编译,并希望从typescript文件中删除Visual Studio构建步骤.
通常我不在乎它是否正在构建它们,但我使用的是Typescript> 1.8.4,它具有Visual Studio无法理解的语言功能,这使得Visual Studio会抛出错误并阻止编译.我在这个github问题线程中找到了一个解决方法,但我有其他开发人员正在研究这个并尝试协调一个hack来使其中的代码无法工作.
我也尝试从.csproj文件中删除typescript导入行,但每当我添加一个新的ts文件时,它都会重新添加该行.
有没有办法完全关闭Visual Studio中的打字稿编译/解析步骤并防止它返回?
这在VS 2015中.
在Angular 2的早期RC版本中,我能够通过添加注入窗口对象
{provide: Window, useValue: window}
到提供者数组.
由于升级到角度2(2.1.0)的最新稳定版本,现在抛出控制台错误
compiler.umd.js:14268Uncaught错误:无法解析LoginComponent的所有参数:(AuthService,UserMessagesService,?).
的?在参数列表中是我试图注入Window对象的地方.
我有一个角度2应用程序,并希望拦截路线事件,防止它们发生,播放动画,然后继续路由.
我的想法是这样的
如果我有课
export class SomeComponent {
constructor(private router: Router){
router.events.subscribe(evt => {
if(evt instanceof NavigationStart){
//I would like to cancel the event here the first time, and then
//play an animation, after the animation is done, trigger the router
//to go to the original route it wanted to.
}
})
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法阻止该路由器完成导航过程?
我创建了一个源自此博客文章的Angular组件.我有一个被动的形式,我想在窗体控件上得到错误,组件本身将有一个程式化的错误消息,它将在控件有错误时呈现.但是,当我尝试将NgControl类注入组件时,我得到循环引用问题,那么我将如何访问控件上的错误?
这是当前的代码,它还没有完成,但它应该给出我想要完成的基本想法:
import { Component, Output, EventEmitter, Input, forwardRef } from '@angular/core';
import {
NgControl,
NG_VALUE_ACCESSOR,
ControlValueAccessor,
Validator,
AbstractControl,
FormControl,
NG_VALIDATORS
} from '@angular/forms';
@Component({
selector: 'form-field-input',
templateUrl: './form-field-input.component.html',
styleUrls: ['./form-field-input.component.less'],
providers: [{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => FormFieldInputComponent),
multi: true
}]
})
export class FormFieldInputComponent implements ControlValueAccessor {
private propagateChange = (_: any) => { };
private propagateTouch = (_: any) => { };
@Input('label') label: string;
@Input('type') type: string;
@Input('id') id: string;
@Input('formControlName') formControlName: string;
@Input('error') …Run Code Online (Sandbox Code Playgroud) 我已经看到了BHO扩展,JavaScript可以在C++ BHO中调用函数.但是让我说我没有使用BHO,而是我有一个C++控制台应用程序,它创建一个IE COM对象,如下所示:
HRESULT hr = CoCreateInstance(
CLSID_InternetExplorer,
NULL,
CLSCTX_LOCAL_SERVER,
IID_IWebBrowser2,
(void**)&_cBrowser);
Run Code Online (Sandbox Code Playgroud)
我还有一个"拥有"从该函数返回的IWebBrowser2对象的类.
class BrowserWrapper{
public:
CComPtr<IWebBrowser2> pBrowser;
void SomeFunction(...)
}
Run Code Online (Sandbox Code Playgroud)
有没有办法从生成的IWebBrowser2对象中的JavaScript中调用包装类中的"SomeFunction"这样的函数?
如果我在Go中有一片字节,类似于:
numBytes := []byte { 0xFF, 0x10 }
Run Code Online (Sandbox Code Playgroud)
我如何将其转换为它的uint16值(0xFF10,65296)?
Angular 2是否相当于Angular 1的$ interpolate?我想获取一个字符串和一个对象,并能够用该对象的值替换字符串中的标记.
例:
let myTemplateStr = "Hello {{user.name}}, you are {{user.age.years}} years old"
//use the Angular2 equivalent of interpolate
let myTemplateCompiled = Angular2.Interpolate(myTemplateStr, {
user: {
name: "bob",
age: {
years: 10
}
}})
//myTemplateCompiled ends up becoming "Hello bob, you are 10 years old"
Run Code Online (Sandbox Code Playgroud)
我可以写一些能为我做这件事的东西,但如果可能的话,我更愿意使用内置的角度方式.
编辑:
我应该提到插值需要能够用字符串变量发生.我知道typescript/es6对文字字符串有反向插值,但我需要插入存储在变量中的模板字符串.也许有一种方法可以在我不知道的变量中使用插值内置的typescript作为字符串模板?
我可以调用Angular 4中的某些内容,在哪里传入字符串或路由令牌数组并获取该路由的静态路由数据?
例如:
const targetRoute = '/test/route'
const routeData = {{something}}.getRouteData(targetRoute)
// { routeDataValue: 'something' } etc...
Run Code Online (Sandbox Code Playgroud)
我正在寻找的数据是路线定义中定义的数据
const routes: Routes = [
{
path: 'test/route',
data: { //This data object
animation: {
value: 'fetching-results'
},
progress: 100,
sectionIdentifier: {
background: 'results',
backLinkUrl: null,
backLinkText: null
}
},
}
]
Run Code Online (Sandbox Code Playgroud) 我有一个带有以下签名的函数
public async sequenceAnimations(animations: AnimationPlayer[] | AnimationTracker[]): Promise<any>
Run Code Online (Sandbox Code Playgroud)
在函数本身中,我想根据它是 AnimationPlayer 数组还是 AnimationTracker 数组进行分支,所以我尝试了以下方法:
let mappedAnimations = animations;
if (animations instanceof Array<AnimationTracker>) {
mappedAnimations = animations.map(anim => anim.animationPlayer)
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我试图允许调用者传递一个 AnimationPlayer 数组或一个包含 animationPlayer 实例的 AnimationTracker 数组。但是在使用类型检查数组的实例时出现错误
“instanceof”表达式的右侧必须是“any”类型或可分配给“Function”接口类型的类型。
另外,自动完成功能不会在 if 块中注册数组的类型,因此我认为我无法像这样检查数组类型。
确定传递的数组类型的正确方法是什么?