不能设置默认的复选框,同时使用angularjs检查复选框、angularjs

2023-09-13 04:32:53 作者:未来的路只有靠自己

我要复选框的默认值设置为检查即,用户必须看到打开网页出于某种原因,而使用angularjs它不工作时复选框被选中

这是我的code

 < HTML NG-应用>    <输入类型=电台NAME =查找NG模型=查找值=1NG-检查=查找== 1选中>员工查找< /输入>    <输入类型=电台NAME =查找NG模型=查找值=2NG-检查=查找== 2>公司查询与LT; /输入>< / HTML> 

解决方案

由于您使用 ngModel ,收音机会自动它们是否匹配选择。下面是HTML和JS:

< HTML NG-NG应用程序控制器=的TestController>  <输入类型=电台NG-模式=查找值=1>员工查找< /输入>  <输入类型=电台NG-模式=查找VALUE =2>公司查询与LT; /输入>< / HTML>

函数的TestController($范围){  $ scope.lookup = 1;} 添加复选框

这里是一个工作的jsfiddle演示: http://jsfiddle.net/BinaryMuse/ZQDts/3/ (您可能已经有与您的jsfiddle麻烦,因为你拼错 NG-应用

I want to set default value of checkbox to checked ie., user must see that check box is checked when opening the page for some reason it does not work while using angularjs

here is my code

<html ng-app>

    <input type="radio" name="lookup"  ng-model="lookup" value="1" ng-checked="lookup==1" checked>Employee Lookup</input>
    <input type="radio" name="lookup"  ng-model="lookup" value="2" ng-checked="lookup==2">Company Lookup</input>
</html>

解决方案

Since you're using ngModel and value, the radio will automatically be selected if they match. Here is the HTML and JS:

<html ng-app ng-controller="testcontroller">
  <input type="radio" ng-model="lookup" value="1">Employee Lookup</input>
  <input type="radio" ng-model="lookup" value="2">Company Lookup</input>
</html>

function testcontroller($scope){
  $scope.lookup = 1;
}

And here is a working jsFiddle demonstration: http://jsfiddle.net/BinaryMuse/ZQDts/3/ (You might have been having trouble with your jsFiddle because you misspelled ng-app.)