簡體   English   中英

復選框 ng-model 問題

[英]Checkbox ng-model issue

親愛的社區,我需要在復選框中將公司價值帶入 ng-model。我嘗試了不同的方法。 html代碼:

          <table>       

        <tr>
                        <th>Company</th>
                        <th>Contact</th>
                        <th>Country</th>
                    </tr>
                    <tr ng-repeat="d in matilda" ng-class-odd="odd" ng-class-even="even">
                        <td>{{d.Company}}</td>
                        <td>{{d.Contact}}</td>
                        <td>{{d.Country}}</td>
                        <td class="checkbox">
                            <label><input type="checkbox" ng-true-value="d.Company" ng-false-value="''" ng-model="key"></label>
                        </td>
                    </tr>
        </table>
     <div class="row">
                    <div class="col-md-4 col-xs-3">
                        <button class="button" ng-click="get();">View</button>
                    </div>
                    <div class="col-md-4 col-xs-3">
                        <button type="button" class="button" data-toggle="modal" data-target="#exampleModal">
                            Add
                        </button>
                    </div>
                    <div class="col-md-4 col-xs-3">

 <button class="button" ng-click="del();">Delete</button>
                    </div>
                </div>

角度代碼://設置初始值

  angular.module('MyApp', [])
    var app = angular.module('myApp', []);
        app.controller('TableController', ['$scope', '$rootScope', '$http', function ($scope, $rootScope, $http) {
            $scope.getdeat = "";
            $scope.Company = "";
            $scope.Contact = "";
            $scope.Country = "";
            $scope.key = "";

    $scope.get = function () {
        alert("con");

    try {

        $http({
            method: 'POST',
            data: {},
            url: '/Home/Login',
            timeout: httpTimeout,
        }).then(function successCallback(response) {

            $scope.matilda = response.data;

        }, function errorCallback(response) {
            alert("error");
        });
    }
                        catch (ex) {alert(ex); }


            }
            $scope.add = function () {
                alert($scope.Company);
                var reg = { Company: $scope.Company, Contact: $scope.Contact, Country: $scope.Country };
                try {

                    $http({
                        method: 'POST',
                        data: reg,
                        url: '/Home/AddValue',
                        timeout: httpTimeout,
                    }).then(function successCallback(response) {
                        var maxi = response.data;
                        var code = maxi.Code;
                        if (code == "1") {
                            alert("Table Updated");
                        }

                    }, function errorCallback(response) {
                        alert("error");
                    });
                }
                catch (ex) { alert(ex); }


            }
            $scope.del = function () {

                // set initial value

                alert($scope.isChecked);
                try {

                    $http({
                        method: 'POST',
                        data: $scope.key,
                        url: '/Home/AddValue',
                        timeout: httpTimeout,
                    }).then(function successCallback(response) {
                        var maxi = response.data;
                        var code = maxi.Code;
                        if (code == "1") {
                            alert("Table Updated");
                        }

                    }, function errorCallback(response) {
                        alert("error");
                    });
                }
                catch (ex) { alert(ex); }


            }
}]);

警報顯示 '' 和未定義,我是初學者,請指出我在哪里犯了錯誤,我在 ng-repeat 部分或我的 js 部分犯了任何錯誤嗎?

嘗試像這樣設置 ng-true-value

ng-true-value="'{{d.Company}}'"

並且由於您是異步獲取數據,因此建議像這樣設置 ng-if

<table ng-if="matilda">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM