这是一个简单的问题,我有一个简单的HashMap,我想要反转键和值.
HashMap<Character, String> myHashMap = new HashMap<Character, String>();
myHashMap.put('a', "test one");
myHashMap.put('b', "test two");
Run Code Online (Sandbox Code Playgroud)
我想创建一个新的HashMap,我在其中放置了对立面.
HashMap<String, Character> reversedHashMap = new HashMap<String, Character>();
e.g. Keys "test one" & "test two" and values 'a' & 'b'.
Run Code Online (Sandbox Code Playgroud) 所以代码很简单:
calls.json
{"SERVER":{
"requests":{
"one":"1"
}
} }
Run Code Online (Sandbox Code Playgroud)
file.ts
import json = require('../static/calls.json');
console.log(json.SERVER);
Run Code Online (Sandbox Code Playgroud)
生成的javascript是正确的,当运行节点js服务器时,控制台日志json.SERVER打印出'{requests:{one:'1'}}',就像它应该的那样.
但是,打字稿编译器(commonjs),不知何故并不特别喜欢这种情况并抛出:"找不到模块'../static/calls.json'".
当然我试着写一个.d.ts文件,像这样:
declare module '../static/calls.json'{
var exp:any;
export = exp;
}
Run Code Online (Sandbox Code Playgroud)
这显然会抛出:"Ambient模块声明不能指定相对模块名称".
我也尝试了不同的变体,例如:
declare module 'calls.json' {
import * as json from '/private/static/calls.json';
export = json;
}
Run Code Online (Sandbox Code Playgroud)
然后要求:
import json = require('calls.json');
Run Code Online (Sandbox Code Playgroud)
没有正常工作,并有自己的小编译器错误:)
我想使用外部.json文件,因为我使用commonjs serverside和amd clientside,我想要一个文件来加载常量.
所以这是一个二合一的问题.
首先,我试图在组件html中的元素加载时触发函数.我尝试了很多方法,比如:<div [onload]="myFunction()">这会导致函数被多次调用,确切地说是10.我的猜测是这不是要走的路,但我还不够熟悉,无法让它正常工作.另外,我想将元素作为参数发送.例如,做<div #myDiv (click)="myFunction(myDiv)">确实有效,但显然这不会触发所述元素的onload.什么是正确的方式,或者我有义务做一个querySelector ...
接下来是涉及在组件内注入ElementRef的问题.现在,文档告诉我使用'nativeElement'属性不是很好的方法.我真的不明白为什么.在组件中引用元素是一件好事,不是吗?还是我在监督关注事项的分离?我问,因为如果我的第一个问题不是一个选项,我想使用这个元素引用来在OnInit类的ngOnInit函数中执行我想要的onload触发元素的querySelection.
欢迎所有信息,看到angular2文档不完整.谢谢.
export class HomeComponent implements OnInit
{
public categories: Category[];
public items: Item[];
constructor
(
public element: ElementRef,
private _categoryService: CategoryService,
private _itemService: ItemService,
private _router: Router
){}
public registerDropdown(element:HTMLElement): void
{
console.log(element);
}
private getCategories(): void
{
this._categoryService.getAll().then((categories:Category[])=> this.categories = categories);
}
private getItems(): void
{
this._itemService.getAll().then((items:Item[])=> this.items = items);
}
public ngOnInit(): any
{
this.getCategories();
this.getItems();
}
}Run Code Online (Sandbox Code Playgroud)
<div id="home">
<div id="search">
<div class="container">
<!-- …Run Code Online (Sandbox Code Playgroud)示例代码:
class GameListViewModel {
private IGameRepository repository;
public GameViewModel GameViewModel { get; set; }
public ObservableCollection<GameViewModel> Games { get; set; }
public ObservableCollection<GenreViewModel> Genres { get; set; }
public ICommand AddGame_ { get; set; }
public GameListViewModel() {
repository = new DummyGameRepository();
GameViewModel = new GameViewModel();
Games = new ObservableCollection<GameViewModel>(repository.GameList().Select(game => new GameViewModel(game)));
Genres = new ObservableCollection<GenreViewModel>(repository.GameList().Select(game => game.Genre).Distinct().Select(genre => new GenreViewModel(genre)));
AddGame_ = new RelayCommand(AddGame, CanAddGame);
}
}
class Game {
public string Title { get; set; }
public …Run Code Online (Sandbox Code Playgroud)