将模型绑定到选定的值,而不是在项目上设置选定的值

时间:2021-10-16 23:00:33

I have the following code which works fine:

我有以下代码可以正常工作:

<div class="col-md-12">
    <div class="button-group">
        <button type="button" class="btn btn-default btn-block btn-sm dropdown-toggle" data-toggle="dropdown"><span class="">TurbineNumber</span> <span class="caret"></span></button>
        <ul class="dropdown-menu" style="">
            <li style="padding-left:10px;" ng-repeat="c in turbineNumbersDistinct">
                <label style="font-weight:100;">
                    <input type="checkbox" ng-model="c.Selected" ng-change="filterChanged()" />&nbsp;{{c.Name}}
                </label>
            </li>
        </ul>
    </div>
</div>

However I would like to bind the selected value to a new array instead of setting Selected on the object it self. I have found some samples that calls a function that fills the bound array, but isn't it possible to do this inline?

但是,我希望将所选值绑定到一个新数组,而不是在对象本身上设置所选值。我已经找到了一些示例,它们调用一个填充绑定数组的函数,但难道不可能进行内联操作吗?

2 个解决方案

#1


1  

This works, using ng-change you can push or splice the element into the new output array($scope.output).

通过使用ng-change,您可以将元素推入或拼接到新的输出数组中($scope.output)。

JSFiddle Demo

JSFiddle演示

HTML:

HTML:

<div ng-controller='MyController' ng-app="myApp">
    <div class="col-md-12">
    <div class="button-group">
        <button type="button" class="btn btn-default btn-block btn-sm dropdown-toggle" data-toggle="dropdown"><span class="">TurbineNumber</span> <span class="caret"></span></button>
        <ul class="dropdown-menu" style="">
            <li style="padding-left:10px;" ng-repeat="c in turbineNumbersDistinct">
                <label style="font-weight:100;">
                    <input type="checkbox" ng-model="selected[$index]" ng-change="selected[$index] ? output.push(c.Name): output.splice(output.indexOf(c.Name), 1)"  />&nbsp;{{c.Name}}
                </label>
            </li>
        </ul>
    </div>
    {{output}}
</div>
</div>

#2


1  

Do you mean something like that?

你是说那种东西吗?

ng-model="newArray[$index]"

This will bind the input in to the array in the index of the iteration.

这将把输入绑定到迭代索引中的数组。

#1


1  

This works, using ng-change you can push or splice the element into the new output array($scope.output).

通过使用ng-change,您可以将元素推入或拼接到新的输出数组中($scope.output)。

JSFiddle Demo

JSFiddle演示

HTML:

HTML:

<div ng-controller='MyController' ng-app="myApp">
    <div class="col-md-12">
    <div class="button-group">
        <button type="button" class="btn btn-default btn-block btn-sm dropdown-toggle" data-toggle="dropdown"><span class="">TurbineNumber</span> <span class="caret"></span></button>
        <ul class="dropdown-menu" style="">
            <li style="padding-left:10px;" ng-repeat="c in turbineNumbersDistinct">
                <label style="font-weight:100;">
                    <input type="checkbox" ng-model="selected[$index]" ng-change="selected[$index] ? output.push(c.Name): output.splice(output.indexOf(c.Name), 1)"  />&nbsp;{{c.Name}}
                </label>
            </li>
        </ul>
    </div>
    {{output}}
</div>
</div>

#2


1  

Do you mean something like that?

你是说那种东西吗?

ng-model="newArray[$index]"

This will bind the input in to the array in the index of the iteration.

这将把输入绑定到迭代索引中的数组。