我在模板中有一个看起来像这样的表单
<form [ngFormModel]="form">
<div ngControlGroup="contents">
<div ngControlGroup="title">
<div *ngFor="let lang of languages$ | async" [ngControlGroup]="lang">
<md-input ngControl="content"></md-input>
</div>
</div>
</div>
</form>
,形状看起来像这样
this.form = this.builder.group({
contents: this.builder.group({
title: this.builder.group({
en: this.builder.group({
id: this.builder.control(null),
content: this.builder.control('some english content')
}),
cs: this.builder.group({
id: this.builder.control(null),
content: this.builder.control('some czech content')
})
})
})
});
我想把
<div ngControlGroup="contents">
<div ngControlGroup="title">
<div *ngFor="let lang of languages | async" [ngControlGroup]="lang"><!-- --></div>
</div>
</div>
分成一个组件,看起来像这样
<form-contents-item type="title" [languages]="languages$ | async">
<md-input ngControl="content"></md-input>
</form-contents-item>
及其模板
<div ngControlGroup="contents">
<!-- `title` from @Input -->
<div [ngControlGroup]="title">
<!-- `languages` from @Input -->
<div *ngFor="let lang of languages" [ngControlGroup]="lang">
<!-- content projection here, probably using `ngTemplateOutlet`, since you can't iterate over ng-content -->
</div>
</div>
</div>
但问题是,ngControlGroup
需要是ngForm
或ngFormModel
的孩子,ngControl
对我投射的投标template
form
form-contents-item
1}}。所以问题是,我怎么可能做这个工作?将public class Order extends AppCompatActivity {
private ListView OrderList;
Button Mbyll;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private OrderAdapter adapter;
public static final String KEY_ORDER_DATA = "oder_data";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
Mbyll = (Button)findViewById(R.id.mbyll_porosine);
//OrderList = (ListView.(R.id.list);
// ListView listView = (ListView) findViewById(R.id.listView);
Mbyll.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), com.android.restaurant.activity.Home.class);
volley_dergo_rregjistrim();
startActivity(i);
}
});
OrderList = (ListView) findViewById(R.id.listView);
displayData();
}
private void displayData() {
adapter = new OrderAdapter(getApplication(), DatabaseManager.INSTANCE.getAllCartData() );
OrderList.setAdapter(adapter);
}
public void volley_dergo_rregjistrim(){
// Tag used to cancel the request
String url = "My Url";
StringRequest strReq = new StringRequest(Request.Method.POST,
url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Toast.makeText(getApplicationContext(),response,Toast.LENGTH_LONG).show();
try {
//Do it with this it will work
JSONObject obj = new JSONObject(response);
boolean trueOrFalse = obj.getBoolean("error");
if(trueOrFalse){
//String userId = obj.getString("id");
//Toast.makeText(getApplicationContext(),userId.toString(),Toast.LENGTH_LONG).show();
}else{
JSONObject obj1 = new JSONObject(response);
String msg = obj1.getString("message");
Toast.makeText(getApplicationContext(),msg.toString(),Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),"Error 1 :"+e.toString(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"Error2",Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
OrderRequest order = new OrderRequest();
order.setOrder_type("2");
order.setId_reservation(138);
order.fillRestProductList(DatabaseManager.INSTANCE.getAllCartData());
Map<String,String> params=new HashMap<String,String>();
params.put(KEY_ORDER_DATA,new Gson().toJson(order));
return params;
}
};
VolleyApplication.getInstance().getRequestQueue().add(strReq);
}
}
实例共享到我的public class OrderAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<DbProductModel> items;
private FragmentManager fMan;
int index;
public OrderAdapter(Context c, ArrayList<DbProductModel> items ) {
this.mContext = c;
this.items = items;
}
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(final int pos, View child, ViewGroup parent) {
Holder mHolder;
index = pos;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.card_structure, null);
mHolder = new Holder();
mHolder.txt_title = (TextView) child.findViewById(R.id.card_title);
mHolder.txt_price = (TextView) child.findViewById(R.id.card_price);
mHolder.txt_total_price = (TextView) child.findViewById(R.id.card_total_price);
mHolder.txt_total = (TextView) child.findViewById(R.id.total);
mHolder.txt_description = (TextView) child.findViewById(R.id.description_txt);
mHolder.delete_order = (Button) child.findViewById(R.id.delete);
mHolder.add = (Button) child.findViewById(R.id.add);
mHolder.remove = (Button) child.findViewById(R.id.remove);
mHolder.note = (Button) child.findViewById(R.id.btn_add_note);
mHolder.pza_image = (ImageView) child.findViewById(R.id.card_imagee);
child.setTag(mHolder);
} else {
mHolder = (Holder) child.getTag();
}
mHolder.delete_order.setOnClickListener(new View.OnClickListener() {
//DbProductModel products = id_list.get(index);
@Override
public void onClick(View v) {
DatabaseManager.INSTANCE.removeProductFromCart(items.get(pos).getId_dish());
items.remove(pos);
Toast.makeText(v.getContext(), "Delete", Toast.LENGTH_SHORT).show();
notifyDataSetChanged();
}
});
mHolder.add.setOnClickListener(new View.OnClickListener() {
//DbProductModel products = id_list.get(index);
@Override
public void onClick(View v) {
DatabaseManager.INSTANCE.addToCart(items.get(pos));
items.get(pos).setQuantity(items.get(pos).getQuantity() + 1);
Toast.makeText(v.getContext(), "added quantity", Toast.LENGTH_SHORT).show();
notifyDataSetChanged();
}
});
mHolder.remove.setOnClickListener(new View.OnClickListener() {
//DbProductModel products = id_list.get(index);
@Override
public void onClick(View v) {
if (items.get(pos).getQuantity() > 1) {
boolean couldDecrement = DatabaseManager.INSTANCE.decrementQuantityOfProdInCart(items.get(pos).getId_dish());
if (couldDecrement) {
items.get(pos).setQuantity(items.get(pos).getQuantity() - 1);
Toast.makeText(v.getContext(), "decreased quantity", Toast.LENGTH_SHORT).show();
}
notifyDataSetChanged();
}
}
});
mHolder.note.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//NotesDialogFragment.newInstance(items.get(pos).getId_dish()).show(fMan, NotesDialogFragment.class.getSimpleName());
/// Demo.newInstance(items.get(pos).getId_dish());
// showInputDialog(v.getContext());
showInputDialog();
//showBasicLongContent("","titull");
}
});
mHolder.txt_title.setText(items.get(pos).getName());
mHolder.txt_price.setText("Cmimi: "+items.get(pos).getPrice());
mHolder.txt_total_price.setText("Cmimi Total: "+items.get(pos).getPrice_total());
mHolder.txt_total.setText(""+ items.get(pos).getQuantity());
mHolder.txt_description.setText(""+ items.get(pos).getDescription());
Glide.with(mContext)
.load(My Url"+items.get(pos).getImage())
.centerCrop()
.placeholder(R.drawable.placeholder)
.crossFade()
.into(mHolder.pza_image);
return child;
}
public class Holder {
// TextView txt_id;
TextView txt_title;
TextView txt_price;
TextView txt_total_price;
TextView txt_total;
TextView txt_description;
ImageView pza_image;
Button delete_order;
Button remove;
Button add;
Button note;
}
public void showInputDialog() {
new MaterialDialog.Builder(mContext)
.title("TEST")
.content("TEST")
.inputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_PERSON_NAME |
InputType.TYPE_TEXT_FLAG_CAP_WORDS)
.inputRange(2, 16)
.positiveText("TEST")
.input("TEST", "TEST", false, new MaterialDialog.InputCallback() {
@Override
public void onInput(@NonNull MaterialDialog dialog, CharSequence input) {
//showToast("Hello, " + input.toString() + "!");
}
}).show();
}
}
,可能只适用于该组,而不适用于输入本身?还有别的吗?
答案 0 :(得分:0)
通过实施ngModel
,只需创建一个普通组件,使其与ControlValueAccessor
兼容。
答案 1 :(得分:0)
我遇到了类似的问题,我必须做的是在子组件上设置controlGroup,通过子组件中的构造函数注入访问控制组,同时在模板中设置ngFormModel
。 p>
父模板:
<form-contents-item [ngControlGroup]="content" type="title" [languages]="languages$ | async">
<md-input ngControl="content"></md-input>
</form-contents-item>
儿童模板:
<div [ngFormModel]="contents">
<!-- `title` from @Input -->
<div [ngControlGroup]="title">
<!-- `languages` from @Input -->
<div *ngFor="let lang of languages" [ngControlGroup]="lang">
<!-- content projection here, probably using `ngTemplateOutlet`, since you can't iterate over ng-content -->
</div>
</div>
</div>
修改:contents
将是在controlGroup
form-contents-item
分配的属性名称