angularjs只获得选中的复选框

时间:2016-08-06 20:46:59

标签: angularjs checkbox

我希望在我的循环中获取所选复选框,对于该复选框,我必须重新点击金额字段。

这是我的HTML脚本:

#define APPWM_ICONNOTIFY (WM_APP + 1)

...

HICON hIcon = static_cast<HICON>(LoadImage(NULL,
    TEXT("gui\\sample.ico"),
    IMAGE_ICON,
    0, 0,
    LR_DEFAULTCOLOR | LR_SHARED | LR_DEFAULTSIZE | LR_LOADFROMFILE));

SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);

//Notification
NOTIFYICONDATA nid = {};
nid.cbSize = sizeof(nid);
nid.hWnd = hwnd;
nid.uID = 1;
nid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
nid.uCallbackMessage = APPWM_ICONNOTIFY;
nid.hIcon = hIcon;
// This text will be shown as the icon's tooltip.
StringCchCopy(nid.szTip, ARRAYSIZE(nid.szTip), title);

// Show the notification.
Shell_NotifyIcon(NIM_ADD, &nid);

...

LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case APPWM_ICONNOTIFY:
        {
            switch (lParam)
            {
                case WM_LBUTTONUP:
                    //...
                    break;
                case WM_RBUTTONUP:
                    //...
                    break;
            }

            return 0;
        }

        //...
    }

    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

以下脚本显示所有复选框。我想要唯一选择的那个。

JS脚本:

<div ng-repeat="$item in items">
    Amount  :<input ng-model="$item.daily_data.payment_amount">
    Check  : <input type=checkbox ng-model="checkAmount[$item.daily_data.id]" ng-value="$item.id" >
</div>

<input type="button" ng-click="checkNow()" />

1 个答案:

答案 0 :(得分:3)

首先要将functions$scope一起使用,你应该这样做:

$scope.checkNow = function() {
  ...
}

$scope.checkNow = checkNow;

function checkNow() {
  ...
}

关于您的问题:

您可以将checkboxes绑定到属性(类似checked),这样您就可以轻松地将checked项目放在controllercode 1}}。

然后,要计算所有已检查金额的总计,我&#39;建议您使用Array.prototype.filter() + Array.prototype.reduce()

根据您的原创 (function() { angular .module("app", []) .controller('MainCtrl', MainCtrl); MainCtrl.$inject = ['$scope']; function MainCtrl($scope) { $scope.checkNow = checkNow; $scope.checkAmount = {}; $scope.items = [ { "id": 1 }, { "id": 2 }, { "id": 3 } ]; function checkNow() { $scope.total = $scope.items.filter(function(value) { return value.checked; }).reduce(function(a, b) { return a + b.amount; }, 0); } } })();演示

&#13;
&#13;
<!DOCTYPE html>
<html ng-app="app">

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
</head>

<body ng-controller="MainCtrl">
  <div ng-repeat="$item in items">
    <label>
      Amount: <input type="number" ng-model="$item.amount">
    </label>
    <label>
      Check: <input type=checkbox ng-model="$item.checked">
    </label>
  </div>

  <button type="button" ng-click="checkNow()">Check now</button>
  <hr>
  <label for="total">Total</label>
  <input type="number" id="total" disabled ng-model="total">
</body>

</html>
&#13;
$http
&#13;
&#13;
&#13;