简体   繁体   English

在ng-model AngularJS中的表达

[英]Expression in ng-model AngularJS

I need to execute an expression inside ng-model. 我需要在ng-model中执行一个表达式。

I have an array of integers which is shown in a list and input fields needs be generated on click of each item. 我有一个整数数组,显示在列表中,输入字段需要在点击每个项目时生成。 All input fields should carry a generated value of ratio having base as 1. I also need to get the sum of numbers in the input field since the user can change the value. 所有输入字段都应该带有生成的比率,其基数为1.我还需要获取输入字段中的数字总和,因为用户可以更改该值。

选择3个项目并分割比例

选择4个项目并分割比例

My Code for this is 我的代码就是这样

<div class="row" ng-repeat="kpi in SelectedKpiModel">
    <div class="small-2 columns" ng-click="View.RemoveSelectedKPI($index)" style="margin-top:0.5em;border:ridge;padding-bottom: 1em; padding-top:0.5em; text-align:center"><span class="fa fa-scissors"></span></div>
    <div class="small-4 columns" style="margin-top:0.5em;border:ridge;padding-bottom: 1em; padding-top:0.5em; text-align:center">
        {{kpi.id}}
    </div>
    <div class="small-4 columns" style="float:left;margin-top:0.5em;border:ridge; padding:0em">
        <input type="number" value="{{1/SelectedKpiModel.length}}" />
    </div>
</div>

How do I get the count of all the field values if user changes or how do I store the value of each field and retrieve if need ? 如果用户更改或如何存储每个字段的值并检索是否需要,如何获取所有字段值的计数?

I tried like ng-model="{{1/SelectedKpiModel.length}}" but it gave me error. 我试过像ng-model="{{1/SelectedKpiModel.length}}"但它给了我错误。

Thanks. 谢谢。

You can use something like this - 你可以使用这样的东西 -

<input type="number" ng-model="getLength()" />

and your function - 和你的功能 -

$scope.getLength = () => {
    return 1 / $scope.SelectedKpiModel;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM