我正在寻找使用异步值处理HostBinding的最佳方法.
在Angular v2.1.2之前,我可以host在@Directive装饰器中使用这样的属性:
@Directive({
selector: 'img[my-directive]',
host : {
'[alt]' : "alt | async"
}
})
export class MyDirective {
alt: Observable<string>;
}
Run Code Online (Sandbox Code Playgroud)
但看起来这不是预期的行为,因为版本2.1.2修复了它.请参阅不访问视图局部变量或主机表达式中的管道.
现在,在使用AoT编译进行编译时,我得到了Parser Error: Host binding expression cannot contain pipes in Directive.
[ngModel]当通过上使用单向绑定时<input>,在输入中键入字符始终会将字符添加到 的<input>值中。问题是,如果[ngModel]表达式继续返回其现有值,则该<input>值不会刷新。
这是一个简单的例子:
@Component({
selector: 'my-component',
template: `
<input type="text" [ngModel]="foo.bar" />
`
})
export class MyComponent {
private foo = {
bar: 'baz'
};
}
Run Code Online (Sandbox Code Playgroud)
我希望无论用户输入如何,输入始终显示“baz”,但事实并非如此。
我寻找这种行为的原因是针对 ngrx/store/redux 应用程序,其中 an<input>的值应由单向流动的状态确定。我在 Plunker 上创建了一个示例用例,其中 Misko Hevery 的描述不应该是可编辑的。模型确实没有改变,但<input>无论用户输入什么,都会显示出来。
在“No trackBy”部分,它可以正常工作,但这只是因为 Angular 正在重绘所有 DOM 元素,这会强制进行适当的重新评估(但这不是解决方案)。添加disabled或readonly属性<input>对我来说不是一个可接受的答案,因为组件应该不知道不允许更改此字段的潜在复杂状态逻辑。
我在 React Redux 中看到了这种行为,我想知道如果我们无法阻止用户改变自己的视图,我们如何在 Angular 2 中使用单向绑定。
当使用@HostBinding将属性绑定到我的组件的背景图像时,当我指定图像URL时它不起作用.
Angular2 RC-1的第一个例子:
import {Component, HostBinding} from "@angular/core";
@Component({
selector: 'demo',
template: 'something'
})
export class DemoComponent {
@HostBinding('style.background-image')
backgroundImage = 'url(http://placekitten.com/g/200/300)';
}
Run Code Online (Sandbox Code Playgroud)
在检查DOM时,我们可以找到<demo>something</demo> - > NOT GOOD
第二个例子:
import {Component, HostBinding} from "@angular/core";
@Component({
selector: 'demo',
template: 'something'
})
export class DemoComponent {
@HostBinding('style.background-image')
backgroundImage = 'none';
}
Run Code Online (Sandbox Code Playgroud)
这次,在检查DOM时,我们可以找到<demo style="background-image: none;">something</demo> - > GOOD
我尝试使用background而不是background-image,它使用颜色,"blue"但仍然无法使用URL.
我还尝试稍后使用setTimeout内部动态更改值ngAfterViewInit(),它在更改"none"为"blue",但不是使用URL时工作,值保持不变"none".
我尝试使用Symfony捆绑包API-Platform构建API 。
Api资源使用HTTP动词POST,GET,PUT,DELETE提供自动CRUD操作。
我想要的是添加一个端点来处理自定义POST操作,并具有自定义的有效负载/主体,而不依赖于任何资源。
我阻止的地方是将此端点添加到自动API平台文档中。
在GitHub上寻找此类问题时,我发现API平台v2应该可以做到。
似乎有些人找到了使用NelmioApiDoc @ApiDoc批注的方法。