Angular ui-select:如何仅将选定值绑定到ng-model

时间:2022-06-25 19:41:59
$scope.property = new Property();
$scope.property.propertyType = {};

$scope.propertyTypes = [
    { value: 'ResidentialPlot', name: 'Residential Plot' },
    { value: 'CommercialPlot', name: 'Commercial Plot' },
    { value: 'Apartment', name: 'Apartment/Flat' },
    { value: 'Townhouse', name: 'Townhouse' },
    { value: 'House', name: 'Single Family House' },
    { value: 'Commercial', name: 'Commercial Property' }
];

<label for="ptype" class="col-sm-2 control-label">Property Type</label>
<p>Populated: {{property.propertyType}}</p>
<ui-select ng-model="property.propertyType" id="ptype" theme="selectize" ng-disabled="disabled" style="width: 300px;" title="Choose Property Type">
    <ui-select-match placeholder="Select a Property Type">{{$select.selected.value}}</ui-select-match>
    <ui-select-choices repeat="propType in propertyTypes">
        <span ng-bind-html="propType.name"></span>
        <small ng-bind-html="propType.value"></small>    
</ui-select-choices>

This gives me:

这给了我:

$scope.PropertyType = {"value":"Apartment","name":"Apartment/Flat"}

PropertyType in my schema is just a string so I want to bind selected value instead of selected JSON Item.

我的架构中的PropertyType只是一个字符串,因此我想绑定选定的值而不是选定的JSON项。

$scope.PropertyType = "Apartment"

What should I bind to my ng-model to get this?

我应该绑定到我的ng模型来获得这个?

4 个解决方案

#1


11  

You need to change in your select input the ng-model attribute to selected_propertyType and watch it when it changes, then extract value and assign it to propertyType

您需要在选择输入中将ng-model属性更改为selected_propertyType并在更改时观察它,然后提取值并将其分配给propertyType

$scope.property = new Property();
$scope.property.propertyType = {};

$scope.propertyTypes = [
    { value: 'ResidentialPlot', name: 'Residential Plot' },
    { value: 'CommercialPlot', name: 'Commercial Plot' },
    { value: 'Apartment', name: 'Apartment/Flat' },
    { value: 'Townhouse', name: 'Townhouse' },
    { value: 'House', name: 'Single Family House' },
    { value: 'Commercial', name: 'Commercial Property' }
];

$scope.$watch('selected_propertyType',function(newValue,oldValue){
      if (newValue && newValue!=oldValue){
           $scope.propertyType = $scope.selected_propertyType.value;

      }

})


<label for="ptype" class="col-sm-2 control-label">Property Type</label>
<p>Populated: {{property.selected_propertyType}}</p>
<ui-select ng-model="property.selected_propertyType" id="ptype" theme="selectize" ng-disabled="disabled" style="width: 300px;" title="Choose Property Type">
    <ui-select-match placeholder="Select a Property Type">{{$select.selected.value}}</ui-select-match>
    <ui-select-choices repeat="propType in propertyTypes">
        <span ng-bind-html="propType.name"></span>
        <small ng-bind-html="propType.value"></small>    
</ui-select-choices>

#2


34  

You don't need $watch.

你不需要$ watch。

<ui-select ng-model="property.propertyType" id="ptype" theme="selectize" ng-disabled="disabled" style="width: 300px;" title="Choose Property Type">
    <ui-select-match placeholder="Select a Property Type">{{$select.selected.value}}</ui-select-match>
    <ui-select-choices repeat="propType.value as propType in propertyTypes track by $index | filter: $select.search">
        <div ng-bind-html="propType.value | highlight: $select.search"></div>    
</ui-select-choices> 

#3


3  

I had the same problem. I looked up the documentation in:

我有同样的问题。我查阅了以下文档:

https://github.com/angular-ui/ui-select/wiki/ui-select-choices

https://github.com/angular-ui/ui-select/wiki/ui-select-choices

The best way to do this is:

最好的方法是:

<ui-select ng-model="animal.names"> <ui-select-match>{{$select.selected.name}}</ui-select-match> <ui-select-choices group-by="groupFind" repeat="value.id as value in animals | filter: $select.search"> <div ng-bind-html="value.name | highlight: $select.search"></div> </ui-select-choices> </ui-select>

{{$ select.selected.name}}

Note how we are able to specify value.id in repeat while still using value.name for what is shown within the combo box. This will work with ng-model being set to the value.id (whatever was saved).

请注意我们如何在重复中指定value.id,同时仍然使用value.name来显示组合框中显示的内容。这将适用于将ng-model设置为value.id(保存的内容)。

I verified this works for me. Posting here because Google brings people to this page.

我验证这对我有用。在此发布,因为Google会将人们带到此页面。

#4


0  

You can use the select as notation:

您可以使用select作为表示法:

repeat="propType as propType.value for propType in propertyTypes"

#1


11  

You need to change in your select input the ng-model attribute to selected_propertyType and watch it when it changes, then extract value and assign it to propertyType

您需要在选择输入中将ng-model属性更改为selected_propertyType并在更改时观察它,然后提取值并将其分配给propertyType

$scope.property = new Property();
$scope.property.propertyType = {};

$scope.propertyTypes = [
    { value: 'ResidentialPlot', name: 'Residential Plot' },
    { value: 'CommercialPlot', name: 'Commercial Plot' },
    { value: 'Apartment', name: 'Apartment/Flat' },
    { value: 'Townhouse', name: 'Townhouse' },
    { value: 'House', name: 'Single Family House' },
    { value: 'Commercial', name: 'Commercial Property' }
];

$scope.$watch('selected_propertyType',function(newValue,oldValue){
      if (newValue && newValue!=oldValue){
           $scope.propertyType = $scope.selected_propertyType.value;

      }

})


<label for="ptype" class="col-sm-2 control-label">Property Type</label>
<p>Populated: {{property.selected_propertyType}}</p>
<ui-select ng-model="property.selected_propertyType" id="ptype" theme="selectize" ng-disabled="disabled" style="width: 300px;" title="Choose Property Type">
    <ui-select-match placeholder="Select a Property Type">{{$select.selected.value}}</ui-select-match>
    <ui-select-choices repeat="propType in propertyTypes">
        <span ng-bind-html="propType.name"></span>
        <small ng-bind-html="propType.value"></small>    
</ui-select-choices>

#2


34  

You don't need $watch.

你不需要$ watch。

<ui-select ng-model="property.propertyType" id="ptype" theme="selectize" ng-disabled="disabled" style="width: 300px;" title="Choose Property Type">
    <ui-select-match placeholder="Select a Property Type">{{$select.selected.value}}</ui-select-match>
    <ui-select-choices repeat="propType.value as propType in propertyTypes track by $index | filter: $select.search">
        <div ng-bind-html="propType.value | highlight: $select.search"></div>    
</ui-select-choices> 

#3


3  

I had the same problem. I looked up the documentation in:

我有同样的问题。我查阅了以下文档:

https://github.com/angular-ui/ui-select/wiki/ui-select-choices

https://github.com/angular-ui/ui-select/wiki/ui-select-choices

The best way to do this is:

最好的方法是:

<ui-select ng-model="animal.names"> <ui-select-match>{{$select.selected.name}}</ui-select-match> <ui-select-choices group-by="groupFind" repeat="value.id as value in animals | filter: $select.search"> <div ng-bind-html="value.name | highlight: $select.search"></div> </ui-select-choices> </ui-select>

{{$ select.selected.name}}

Note how we are able to specify value.id in repeat while still using value.name for what is shown within the combo box. This will work with ng-model being set to the value.id (whatever was saved).

请注意我们如何在重复中指定value.id,同时仍然使用value.name来显示组合框中显示的内容。这将适用于将ng-model设置为value.id(保存的内容)。

I verified this works for me. Posting here because Google brings people to this page.

我验证这对我有用。在此发布,因为Google会将人们带到此页面。

#4


0  

You can use the select as notation:

您可以使用select作为表示法:

repeat="propType as propType.value for propType in propertyTypes"