0

This should be a very common problem, invoking an ng-model to parse the data into form (DOM) after which the modified checkbox's ng-checked will be translated back to data values so to be saved back on the server.

I have two check boxes respectively

<table><tbody>
        <tr>
            <td align="left">To be ignored</td><td align="left">Yes 
                <input type="checkbox" ng-model="nm_ignore" /></td>
            <td></td>
        </tr>
        <tr>
            <td align="left">To be excluded</td><td align="left">Yes 
                <input type="checkbox" ng-model="nm_exclude" /></td>
            <td></td>
        </tr>
    </tbody>
</table>

And, my data is

$scope.nm_to_ignore = _a_record._ab_ignore; // "T"
$scope.nm_to_exclude = _a_record._ab_x_style; // "F"

My objective is :
I want a straight-forward easy way (easy-to-maintain codewise, which is, angularJS ng-model) to set the checkboxes CHECKED/UNCHECKED by the data read from the server. Also, I want to be able to save the values represented by CHECKED/UNCHECKED to the data just as it came.

4

1 回答 1

0

$scope 的 DOM 中的复选框对象。请注意,我使用属性ng-true-value="'T'"ng-false-value="'F'"告诉翻译器将我的 CHECKED 转换为值“T”,将 UNCHECKED 转换为“F”,以便我的数据库理解的数据存储在服务器上。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="_assets/js-main/angular-1.5.8.min.js"></script>    
<script src="_assets/JS-Main/angular-route-1.5.8.js"></script>
    <!-- <..DOM..stuffs..> -->

    <tr>
        <td ><input type="checkbox" ng-model="nm_exclude" 
              ng-true-value="'T'" ng-false-value="'F'" /></td>
         <td ><input type="checkbox" ng-model="nm_ignore" 
              ng-true-value="'T'" ng-false-value="'F'" /></td>
    </tr>

从服务器获取数据 -->

var A1 = angular.module('_mod_1', ['ngRoute', 'ngSanitize']);
A1.config(function ($locationProvider) {
    $locationProvider.html5Mode(true);
});
A1.controller
('Controller',
    function ($scope, $location, $http) {
        angular.element(document)
            .ready(function () {
                $http.get(_endGet, config)
                 .success(function (serverdata, status, config) {
                          _a_record = serverdata;
                          /*     lines of code        */
                          $scope.nm_ignore = _a_record._ab_ignore; //"T"
                          $scope.nm_exclude = _a_record._ab_x_style; //"T" 
                  });;
            });
        //Things to Do ...
    }
);

希望以上所有对您有所帮助。祝你好运!(如果映射值是非数字的,请注意单引号'T'闭包:ng-true-value=" ",如果您忽略它们,它会给您错误!)

于 2019-01-30T22:03:10.880 回答