简体   繁体   English

Angular js输入框如何通过ng-model而无需更改呢?

[英]Angular js input box how to pass ng-model without change it?

i am trying to push an ng-model value inside an ng-controller array, using the input box. 我正在尝试使用输入框在ng-controller数组中推送ng-model值。

It seems that when i check the box the ng-model propriety change: 似乎当我选中ng-model所有权更改框时:

HERE THE PROBLEM 这里的问题

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

I just want that ng-model propriety won't change when the input is checked, here my code 我只希望检查输入时ng-model的适当性不会改变,这是我的代码

json model json模型

[
 {
    "nomeservizio" : "Frameworks",
    "framewrok":[
        {
            "name":"nessuno",
            "costo": 40
        },
        {
            "name":"bootstrap",
            "costo": 0
        }
    ]
}]

HTML HTML

    <div class="row" ng-repeat="voce in voices.data">
    <h4 style="color:#000;">{{voce.nomeservizio}}</h4>
    <div ng-repeat="cssframework in voce.framewrok">
        <input type="checkbox" ng-model="cssframework.costo"  ng-change="AggiornaTotale({{cssframework.costo}})"/>  
        <span>{{cssframework.name}}........<b>{{cssframework.costo | currency}}</b></span>
    </div>  
</div>

<div class="row">
    <h3>TOTALE: {{selectedVoices}}</h3>
</div>

JS INSIDE CONTROLLER JS内置控制器

    $scope.AggiornaTotale = function(param) {
    $scope.selectedVoices = [];
    $scope.selectedVoices.push(param);
}   

Model Name for value and checkbox model should not be the same. 值的模型名称和复选框模型不应相同。

when you change checkbox - the model inside cssframework object also is updated. 更改复选框时-cssframework对象内部的模型也会更新。

Please try something like that (look at model in checkbox input): 请尝试类似的操作(在复选框输入中查看模型):

<div ng-repeat="cssframework in voce.framewrok">
        <input type="checkbox" ng-model="costo"  ng-change="AggiornaTotale({{cssframework.costo}})"/>  
        <span>{{cssframework.name}}........<b>{{cssframework.costo | currency}}</b></span>
</div>  

At the end i think that ng-model in checkbox is unnecessary. 最后,我认为复选框中的ng-model是不必要的。 You don`t use it in that example. 在该示例中,您不会使用它。

Well this solution provides: 这个解决方案可以提供:

  • multiple value from checkboxes and inputs inside an array 复选框和数组内输入的多个值
  • Values are stored to the $scope.selectedVoices as array when clicking the input 单击输入时,值作为数组存储到$ scope.selectedVoices中

Why ever you want that kind of logic. 为什么你想要那种逻辑。 This solution provides all stuff you talked about. 该解决方案提供了您讨论的所有内容。

  myApp = angular.module('myApp', []); myApp.controller('testController', function ($scope) { $scope.selectedVoices = []; $scope.framework = [{ "name":"nessuno", "costo": 40 }, { "name":"bootstrap", "costo": 0 }, { "name":"bootstrap", "costo": 20 } ]; $scope.click = function (key) { $scope.selectedVoices.push($scope.framework[key].costo); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp" ng-controller="testController"> <div ng-repeat="(key, item) in framework"> <input type="checkbox" ng-click="click(key)" /> <span>{{item.name}} {{item.costo | currency}}</b></span> </div> <h1> {{ selectedVoices }}</h1> </div> 

Basically what checkbox box does is that on check it makes ng-model value true and on uncheck ng-model value false. 基本上,复选框的作用是选中时将ng-model值设为true,而取消选中ng-model值则设为false。 You can use ng-true-value to set value says 40 for check box when it is checked. 选中该复选框时,可以使用ng-true-value设置值为40。 Along with this used ng-model value as shown below: 连同此使用的ng-model值,如下所示:

 <div class="row" ng-repeat="voce in voices.data">
    <div ng-repeat="cssframework in voce.framewrok">
        <input type="checkbox" ng-model="cssframework.costo[$index]" ng-true-value="voce"/>  
    </div>  
</div>

So that when to get the value of "cssframework.costo" in controller like this console.log($scope.cssframework.costo) you will get a array. 这样,当在像console.log($ scope.cssframework.costo)这样的控制器中获取“ cssframework.costo”的值时,您将获得一个数组。

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

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