Angular2:传递给子组件的ng-content属性

时间:2016-04-17 00:03:25

标签: angular parameter-passing projection

这样的事情可能吗?

我想通过" hasfocus"从cjc-box到ng-content属性的变量到cjc-input组件。

app.component.html

Thread : Crashed: com.apple.main-thread
0  Trenìt!                       0x10004a2e4 specialized MasterViewController.tableView(UITableView, cellForRowAtIndexPath : NSIndexPath) -> UITableViewCell (MasterViewController.swift:389)
1  Trenìt!                       0x100046488 @objc MasterViewController.tableView(UITableView, cellForRowAtIndexPath : NSIndexPath) -> UITableViewCell (MasterViewController.swift)
2  UIKit                          0x18c6b239c -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 544
3  UIKit                          0x18c6a6fc4 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2360
4  UIKit                          0x18c49cc60 -[UITableView layoutSubviews] + 172
5  UIKit                          0x18c3b9874 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 572
6  QuartzCore                     0x18bd11d58 -[CALayer layoutSublayers] + 168
7  QuartzCore                     0x18bd0c944 CA::Layer::layout_if_needed(CA::Transaction*) + 320

CIC-box.component.html

<div cjc-box><div cjc-input></div></div>

CIC-input.component.html

<div class="cjc-box">
  <div><ng-content hasfocus="focus"></ng-content></div>
</div>

对于ng2中的预测,这是否可能?

1 个答案:

答案 0 :(得分:8)

可以将变量传递给投影内容(假设组件//region - Create CSV file writer = new CSVWriter(new FileWriter(sdCardPath + "Orders_Summary_Report.csv"), ',', CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.DEFAULT_ESCAPE_CHARACTER, "\r\n"); //writer.writeNext(CSVcursor.getColumnNames()); //Write the column headings first before the looping starts String [] headings = ("Order Id,Item ID,Item Name,Item Price,Item Count,Item Total,Paid Amount,VOID Order,Payment Method,Order Signature,Member/Authorization,Tab Number,Order Time").split(","); writer.writeNext(headings); for(CSVcursor.moveToFirst(); !CSVcursor.isAfterLast(); CSVcursor.moveToNext()) //Loop through all results (use row count of table) { String[] entries = (CSVcursor.getInt(CSVcursor.getColumnIndex("orderId"))+ "," +CSVcursor.getInt(CSVcursor.getColumnIndex("itemId"))+ "," +CSVcursor.getString(CSVcursor.getColumnIndex("itemName"))+ "," +CSVcursor.getString(CSVcursor.getColumnIndex("itemPrice")) + "," +CSVcursor.getInt(CSVcursor.getColumnIndex("itemCount"))+ "," +CSVcursor.getString(CSVcursor.getColumnIndex("itemTotal"))+ "," +CSVcursor.getString(CSVcursor.getColumnIndex("orderPaid"))+ "," +CSVcursor.getInt(CSVcursor.getColumnIndex("orderVoid"))+ "," +CSVcursor.getString(CSVcursor.getColumnIndex("orderType"))+ "," +CSVcursor.getBlob(CSVcursor.getColumnIndex("orderSignature"))+ "," +CSVcursor.getString(CSVcursor.getColumnIndex("referenceId"))+ "," +CSVcursor.getInt(CSVcursor.getColumnIndex("orderTab"))+ "," +CSVcursor.getString(CSVcursor.getColumnIndex("orderTime"))).split(","); //Split the string by the delimiter writer.writeNext(entries); //Write current row of DB to file writer.flush(); //Flush the stream } writer.close(); 声明属性cjc-box而组件focus声明属性cjc-input):

hasfocus

这是单向绑定,如果你想要双向绑定,它会稍微复杂一些:

  • <div cjc-box #box><div cjc-input [hasfocus]="box.focus"></div></div> 装饰器添加到box组件的@Input()属性。
  • focus装饰器添加到输入组件的@Input()属性
  • hasfocus添加到输入组件。
  • 在输入组件中@Output() hasfocusChange:EventEmitter<any> = new EventEmitter<any>();更改后添加this.hasfocusChange.emit(this.hasfocus);
  • 将模板更改为hasfocus