在angular2中设置下拉列表的选定值

时间:2017-07-26 11:03:49

标签: html angular angular-reactive-forms

vatCodeList是字符串代码的错误。示例:[' 34u' ,' 23' ' TT'] 需要在那里设置所选值。

<select class="custom-select" formControlName="vatCode">             
            <option *ngFor="let i of vatCodeList">{{i}}</option>          
</select>

3 个答案:

答案 0 :(得分:1)

*.component.ts

public vatCode: any;

*.component.ts内,您可以将vatCode的值设置为vatCodeList中包含的值之一,这将更新所选值。

*.component.html

<select class="custom-select" formControlName="vatCode" [(ngModel)]="vatCode">             
  <option *ngFor="let i of vatCodeList">{{i}}</option>          
</select>

答案 1 :(得分:0)

您可以像这样绑定值属性

from keras.applications.inception_v3 import InceptionV3, preprocess_input
from keras.preprocessing import image
from keras.models import Model
from keras.models import model_from_json
from keras.layers.core import Flatten

from sklearn.preprocessing import LabelEncoder
import numpy as np
import glob
import cv2
import h5py
import os
import json
import cPickle
import datetime

with open('conf/conf.json') as f:    
    config = json.load(f)

model_name = config["model"]
weights = config["weights"]
include_top = config["include_top"]
train_path = config["train_path"]
features_path = config["features_path"]
labels_path = config["labels_path"]
test_size = config["test_size"]
results = config["results"]
model_path = config["model_path"]

base_model = InceptionV3(weights=weights)
model = Model(input=base_model.input, output=base_model.output)
image_size = (299, 299)

print "[INFO] successfully loaded base model and model..."

train_labels = os.listdir(train_path)

print("[INFO] encoding labels...")
le = LabelEncoder()
le.fit([tl for tl in train_labels])

features = []
labels   = []

for i, label in enumerate(train_labels):
    cur_path = train_path + "/" + label
    label = "human"
    for image_path in glob.glob(cur_path + "/*.jpg"):
        img = image.load_img(image_path, target_size=image_size)
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
        feature = model.predict(x)s
        flat = feature.flatten()
        features.append(flat)
        labels.append(label)
        print "[INFO] processed - {}".format(i)
    print "[INFO] completed label - {}".format(label)

targetNames = np.unique(labels)
le = LabelEncoder()
le_labels = le.fit_transform(labels)

print "[STATUS] training labels: {}".format(le_labels)
print "[STATUS] training labels shape: {}".format(le_labels.shape)

h5f_data = h5py.File(features_path, 'w')
h5f_data.create_dataset('dataset_1', data=np.array(features))

h5f_label = h5py.File(labels_path, 'w')
h5f_label.create_dataset('dataset_1', data=np.array(le_labels))

h5f_data.close()
h5f_label.close()

model_json = model.to_json()
with open(model_path + str(test_size) + ".json", "w") as json_file:
    json_file.write(model_json)

model.save_weights(model_path + str(test_size) + ".h5")
print("[STATUS] saved model and weights to disk..")

print "[STATUS] features and labels saved.."

答案 2 :(得分:0)

您可以尝试将表达式放入选项标记以创建选项selected

<select class="custom-select" formControlName="vatCode">             
            <option *ngFor="let i of vatCodeList" {{i == vatCode?'selected':'' }}>{{i}}</option>          
</select>

变量应该引用InputControl的值。使用反应形式可以很容易地提取值并将其表达。

使用ngModel将元素绑定到模型的最简单方法,但您可以检查this解决方案是否有帮助。