Angularjs ngSelected未在select元素中工作

Angularjs ngSelected未在select元素中工作,angularjs,Angularjs,我希望输入中的值过滤下拉列表中的值,对于$index==0的选项,选择的ng为true 当过滤器执行操作时,ng selected中的值将更改为true,元素上的selected属性不会更改为selected=“selected”。如果删除模型,它将按预期工作 <div ng-app ng-controller="AppCtrl" ng-init="sigStyles.companies = ['facebook', 'twitter', 'tumblr', 'myspace']">

我希望输入中的值过滤下拉列表中的值,对于$index==0的选项,选择的ng为true

当过滤器执行操作时,ng selected中的值将更改为true,元素上的selected属性不会更改为selected=“selected”。如果删除模型,它将按预期工作

<div ng-app ng-controller="AppCtrl" ng-init="sigStyles.companies = ['facebook', 'twitter', 'tumblr', 'myspace']">

    <input type="text" ng-model="socialSearch"></input>

    <select>
        <option ng-repeat="company in sigStyles.companies | filter:socialSearch" value="{{company}}" ng-selected="{{$index === 0}}">{{company}}</option>
    </select>

</div>

{{公司}
是否有人知道如何更改选定的属性,或者是否有人知道如何进行搜索输入以根据其输入从选择中删除选项


链接到my fiddle。

我们可以使用搜索模型作为筛选器的参数来创建一个新的已筛选数组。此数组用于ng select。搜索模型中的任何更改都将被监视并用于更新过滤后的数组

<div ng-app="myapp">
    <div ng-controller="ctrlParent">
        <input type="text" ng-model="search" />
        <select ng-model="storedModel" ng-options="opt for opt in filteredArray | filter:search"></select>
    </div>
</div>

工作溶液的杂乱无章。

所以上面的答案是正确的,如果不是有点冗长的话。我不建议使用手表,因为我觉得它有点麻烦。因此,基本上我是这么想的(如果我错过了什么,请随时告诉我,但请记住我正在努力帮助:-)这一个我忘了包括列表,但它可以像第二个一样包括在内

基本上JS看起来像这样

angular.module('myApp', []);

function MainCtrl($scope) {
    $scope.options = ['as', 'b', 'c', 'a2'];
    $scope.opt1 = 0;  
    $scope.selectedOpt = $scope.options[0];
    $scope.setOption = function() {
        if($scope.options.indexOf($scope.inFilter)  > -1){
              $scope.selectedOpt = $scope.inFilter
        };
    }
}
设置选项只是检查文本是否存在,如果存在,则设置值

如果您想在没有匹配项的情况下显示消息,我可能会推荐更多这样的方法


希望这有帮助

谢谢,但问题是,如果我在搜索中键入数组的子字符串,setOption中的if语句将为false,selectedOpt将保留在已过滤掉的选项[0]中。因此,我需要一个条件来说明“将selectedOpt设置为新筛选数组的索引0。确实如此,我现在更新了,当您键入时,它也会重新选择第一个。仅此而已。”
angular.module('myApp', []);

function MainCtrl($scope) {
    $scope.options = ['as', 'b', 'c', 'a2'];
    $scope.opt1 = 0;  
    $scope.selectedOpt = $scope.options[0];
    $scope.setOption = function() {
        if($scope.options.indexOf($scope.inFilter)  > -1){
              $scope.selectedOpt = $scope.inFilter
        };
    }
}