【问题标题】:Update/Assign ng-model object with expression in AngularJs使用 AngularJs 中的表达式更新/分配 ng-model 对象
【发布时间】:2017-04-23 07:37:55
【问题描述】:

我在我的应用程序中使用 MEAN 堆栈,并将 AngularJS 作为我的前端。如何在 Agularjs 中将 ng-bind 的 totalsum 值转换为另一个 input ,实际上我们有一个表格,我们使用过滤器来获取 Sprice 的总和值,然后我们也得到了 sprice 总和值,如 @ 987654328@,那么我们真正期望的是我们需要将bind 这个totalsum 值转换为另一个ng-module 输入...... 例如:- sprice totalsum 值是250,期待ng-bind 值的答案是totalsum value,如250 ...My Plunker。我们不知道我们在哪里做错了,所以请查看 plunker 并帮助我们..

  • 我们有一个表格,在表格中我们使用了过滤功能来获取sprice 的总和值,我们使用<td>{{resultValue | sumOfValue:'sprice'}}</td> 得到了类似250 的答案。

  • 我们期望我们需要将 bind 的总和值 sprice 到另一个 ng-module 像 sprice_total 输入..期待答案 250...

  • 我已将 plunker 作为参考 plunker 请任何知道解决方案的人帮助我们。

我的控制器:- 总价过滤器:-

    .filter('sumOfValue', function () {
return function (data, key) {
    debugger;
    if (angular.isUndefined(data) && angular.isUndefined(key))
        return 0;        
    var sum = 0;

    angular.forEach(data,function(v,k){
      if(v.confirm=='cancelled'){
        sum = sum + parseFloat(v[key]);
      }
    });        
    return sum.toFixed(2);
}

})

我的 HTML:-

   <tr ng-repeat="ram in resultValue=(order.orderfood)  | filter: {confirm: '!cancelled'}">

                <td>{{$index + 1}}</td>
                <td>{{ram.sprice }}</td>


               </tr>
               <tr>
                 <td>sum</td>

                 <td>{{resultValue | sumOfValue:'sprice'}}</td>

               </tr>

我尝试使用 ng-bind 将值获取到另一个输入,例如:-

<input type="text" ng-model="sprice_total" ng-bind="sprice_total={{resultValue | sumOfValue:'sprice'}}">
  • 请更新 plunker 以了解解决方案...谢谢..

【问题讨论】:

    标签: html css angularjs meanjs


    【解决方案1】:

    已创建 myModelValue 指令,将您的表达式分配给 ng-model 对象

    以下为参考link

    var app = angular.module('plunker', []);
    
    app.directive('myModelValue', function () {
        return {
            restrict: 'A',
            require: 'ngModel',
            scope: {
                model: '=ngModel'
            },
            link: function (scope, element, attr, controller) {
                attr.$observe('myModelValue', function (finalValue) {
                    scope.model = finalValue;
                });
            }
        };
    });
    app.filter('sumOfValue', function () {
        return function (data, key) {
            debugger;
            if (angular.isUndefined(data) && angular.isUndefined(key))
                return 0;
            var sum = 0;
    
            angular.forEach(data, function (v, k) {
                if (v.confirm == 'cancelled') {
                    sum = sum + parseFloat(v[key]);
                }
            });
            return sum.toFixed(2);
        }
    }).controller('MainCtrl', function ($scope) {
        $scope.order =
      {
          "_id": "5836b64083d9ce0f0078eae8",
          "user": {
              "_id": "579bdf6123f37f0e00a40deb",
              "displayName": "Table 1"
          },
          "__v": 8,
          "total": "1824",
          "ordercar": [],
          "orderfood": [
          {
              "qty": "1",
              "confirm": "placed",
              "sprice": 250,
              "price": 250,
              "customise": "With Onion,Without Onion",
              "name": "Baasha Pizza"
          },
          {
              "qty": "1",
              "confirm": "cancelled",
              "sprice": 250,
              "price": 250,
              "customise": "With Onion,Without Onion",
              "name": "Baasha Pizza"
          }
          ],
          "phone": null,
          "order_source": "",
          "comment": "",
          "payment_mode": "",
          "nop": null,
          "rating": null,
          "bill": false,
          "complete": false,
          "laundry": false,
          "clean": false,
          "roomservice": false,
          "napkin": false,
          "waiter": false,
          "water": false,
          "name": "fgg",
          "created": "2016-11-24T09:43:28.413Z",
          "isCurrentUserOwner": true
      }
    });
    /* Put your css in here */
    
    body {
        font-size: 14px;
    }
    
    table
    {
        border-collapse:collapse;
    }
    table, td, th
    {
        border:1px solid black;
    }
    td{
        padding: 2px;
    }
    .servicetaxinclusivetrue:before{
      color: green!important;
      content: "\f00c";
    }
    .servicetaxinclusivefalse:before{
      color: red!important;
      content: "\f00d";
    }
    .servicetaxexclusivetrue:before{
      color: green!important;
      content: "\f00c";
    }
    .servicetaxexclusivefalse:before{
      color: red!important;
      content: "\f00d";
    }
    <!DOCTYPE html>
    <html ng-app="plunker">
    
    <head>
        <meta charset="utf-8" />
        <title>AngularJS Plunker</title>
        <script>document.write('<base href="' + document.location + '" />');</script>
        <link rel="stylesheet" href="style.css" />
        <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css">
        <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>    
    </head>
    <body ng-controller="MainCtrl">
        <table ng-table="tableParams" class="table table-bordered ">
            <thead>
                <tr>
                    <th rowspan="2">s.no</th>
                    <th rowspan="2"> sprice </th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="ram in resultValue=(order.orderfood)  | filter: {confirm: '!cancelled'}">
                    <td>{{$index + 1}}</td>
                    <td>{{ram.sprice }}</td>
                </tr>
                <tr>
                    <td>sum</td>
                    <td>{{resultValue | sumOfValue:'sprice'}}</td>
                </tr>
        </table>
        <input type="text" ng-model="sprice_total" my-model-value="{{resultValue | sumOfValue:'sprice'}}">
    </body>
    </html>

    希望这是你所期望的

    【讨论】:

    • 嗨,先生,你好吗……!!!谢谢你的宝贵回答你真的很棒......你一次又一次地帮助我我永远不会忘记你先生......非常感谢你......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-17
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    相关资源
    最近更新 更多