无法将范围变量数据发送到angular js中的复选框

时间:2019-04-27 08:28:56

标签: javascript html angularjs

我想动态设置复选框的true或false,但是在将数据从控制器传递到ng-model时,复选框的状态没有改变。

代码

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
 <input type="checkbox" ng-model={{applicable_status}}>
 <label>Applicable</label>
 <script>
 var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.applicable_status = "true";
});
</script>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

使用ng-init指令初始化应用程序数据。

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl" ng-init="init()">
 <input type="checkbox" ng-model=applicable_status>
 <label>Applicable</label>
   <pre ng-bind="applicable_status | json"></pre>

 <script>
 var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
   $scope.init = function(){
       $scope.applicable_status = true;
   };
});
</script>
</body>
</html>

答案 1 :(得分:0)

您无需在代码中使用ng-init

您只需要从“ true”中删除这些双引号,只需将正常的true值分配给变量,即可正常运行

从以下位置更改代码:

$scope.applicable_status = "true";

收件人

$scope.applicable_status = true;

它将正常工作!

Check this JsBin