当我尝试将[]字节编组为JSON格式时,我只得到一个奇怪的字符串.
请查看以下代码.
我有两个疑问:
如何将[]字节编组为JSON?
为什么[]字节成为这个字符串?
package main
import (
"encoding/json"
"fmt"
"os"
)
func main() {
type ColorGroup struct {
ByteSlice []byte
SingleByte byte
IntSlice []int
}
group := ColorGroup{
ByteSlice: []byte{0,0,0,1,2,3},
SingleByte: 10,
IntSlice: []int{0,0,0,1,2,3},
}
b, err := json.Marshal(group)
if err != nil {
fmt.Println("error:", err)
}
os.Stdout.Write(b)
}
Run Code Online (Sandbox Code Playgroud)
输出是:
{"ByteSlice":"AAAAAQID","SingleByte":10,"IntSlice":[0,0,0,1,2,3]}
Run Code Online (Sandbox Code Playgroud)
golang playground:https://play.golang.org/p/wanppBGzNR
我想使用ngFor trackBy索引。我发现如下所示的方式
@Component({
selector: 'my-app',
template: `
<ul>
<li *ngFor="let item of collection;trackBy: trackByFn">{{item.id}}</li>
</ul>
<button (click)="getItems()">Refresh items</button>
`,
})
export class App {
constructor() {
this.collection = [{id: 1}, {id: 2}, {id: 3}];
}
getItems() {
this.collection = this.getItemsFromServer();
}
getItemsFromServer() {
return [{id: 1}, {id: 2}, {id: 3}, {id: 4}];
}
trackByFn(index, item) {
return index; // or item.id
}
}
Run Code Online (Sandbox Code Playgroud)
但是我发现的所有方式都需要在组件类中创建一个函数。
我尝试了这些,但似乎不起作用:
*ngFor="let item of collection; trackBy:index"
*ngFor="let item of collection; let i = index; trackBy:i"
Run Code Online (Sandbox Code Playgroud)
没有自定义功能,有什么方法可以按索引进行跟踪吗? …
有一个例子
@Component({
selector: 'my-app',
template: `
<div>
<h2 style="background-color:green">
No Filter
</h2>
</div>
<div style="filter:brightness(0.5)">
<h2 style="background-color:green">
Filter with style.
</h2>
</div>
<div [style.filter]="'brightness(' + val + ')'">
<h2 style="background-color:green">
Filter with style binding.
</h2>
</div>
<p>filter binding express value: {{'brightness(' + val + ')'}}</p>
`,
})
export class App {
val = 0.5;
}
Run Code Online (Sandbox Code Playgroud)
https://plnkr.co/edit/gD9xkX5aWrdNDyD6fnIh?p=preview
得到了渲染结果:
似乎样式绑定[style.filter]不起作用。有谁知道原因或通过组件成员值提供其他方法来控制滤镜亮度样式?
感谢您的回答!