如何在Angular上的复选框检查中更新表中的字段?

时间:2016-01-28 05:06:12

标签: ruby-on-rails angularjs

我有一个任务,当我选中复选框时,数据库表中的字段必须更改为TRUE(布尔字段)并取消选中,它应该为FALSE。

这是我的角度html页面复选框代码:

<checkbox-field attribute='updateExistingItems' label='Update Existing Items'></checkbox-field>

和angular / app / src / controllers / file.js:

(function () {
  angular.module('myex').controller('FileImportsCtrl', [
    '$scope', '$rootScope', '$filter', '$state', '$timeout', '$confirm', '$stateParams', '$interval', 'User', 'FileImport', 'fileImports', 'APP_SETTINGS', 'Title', '$modal',
    function ($scope, $rootScope, $filter, $state, $timeout, $confirm, $stateParams, $interval, User, FileImport, fileImports, APP_SETTINGS, Title, $modal) {

    $scope.updateExistingItems = [];
        FileImport.query().then(function (results) {
        return $scope.updateExistingItems = results;
      });
    }
  ]);

}).call(this);

我尝试添加角度控制器,但无法实现。请帮我看看如何编写oncheck并取消选中rails in angular中的代码?

1 个答案:

答案 0 :(得分:1)

正如AddWeb Solution Pvt Ltd所说,您需要使用ng-change以及在复选框上设置ng-model。

<checkbox-field ng-change="updateItems()" ng-model="mycheckbox" attribute='updateExistingItems' label='Update Existing Items'></checkbox-field>

此外,您还需要具有一个功能,可以通过控制器中的ng-change进行调用。像这样:

$scope.updateExistingItems = [];

FileImport.query().then(function (results) {
  $scope.updateExistingItems = results;
});

$scope.updateItems = function(){
  for( var i = 0; i < $scope.updateExistingItems.length; i++){
    $scope.updateExistingItems[i].checkbox = $scope.mycheckbox;
  }      
}

如果您需要访问复选框值,可以使用控制器中的$scope.mycheckbox访问它。