Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript angular中的Checkbox指令_Javascript_Jquery_Angularjs_Checkbox - Fatal编程技术网

Javascript angular中的Checkbox指令

Javascript angular中的Checkbox指令,javascript,jquery,angularjs,checkbox,Javascript,Jquery,Angularjs,Checkbox,我希望使用以下结构为复选框输入创建指令: <label for="sameID" class="style"> <input type="checkbox" id="sameID" name="name" /> <span>some text here</span> </label> 这里有一些文字 然后在我的html中放入像这样的简单标记。 这个指令应该为我处理checked事件。我可以在代码中检查这个复选框是否被选中

我希望使用以下结构为复选框输入创建指令:

<label for="sameID" class="style">
  <input type="checkbox" id="sameID" name="name" />
  <span>some text here</span>
</label>

这里有一些文字
然后在我的html中放入像
这样的简单标记。 这个指令应该为我处理checked事件。我可以在代码中检查这个复选框是否被选中

以下是我尝试创建此指令时的js代码:

app.directive("checkbox", function($compile){

  var check = "{{check}}";
  if (check == true) {
    var inputCheck = '<input class="checkbox" checked type="checkbox" ng-change="isChecked()" name="{{for}}" id="{{for}}" />';
  } else {
    var inputCheck = '<input class="checkbox" ng-change="isChecked()" type="checkbox" name="{{for}}" id="{{for}}" />';
  }
  var temp = '<label for="{{for}}" class="hello">'+inputCheck+' <svg width="100%" v-pressable id="checkboxsvg" height="100%" viewBox="0 0 23 23" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve">'+'<g transform="matrix(1,0,0,1,-262.373,-219.494)">'+'<g id="checkbox-background" transform="matrix(1,0,0,1,73.0395,6.16767)"><path d="M211.114,216.192C211.114,215.089 210.218,214.192 209.114,214.192L192.199,214.192C191.095,214.192 190.199,215.089 190.199,216.192L190.199,233.107C190.199,234.211 191.095,235.107 192.199,235.107L209.114,235.107C210.218,235.107 211.114,234.211 211.114,233.107L211.114,216.192Z"/></g><g id="checkbox-check" transform="matrix(1,0,0,1,35.2522,0.0159298)"><path  d="M234.362,231.155L237.395,234.188L243.299,228.283"/></g></g></svg><span>{{p}}</span></label>';

  return {
    restrict: "E",
    template : temp,
    scope : {},
    link : function(scope, element, attrs, ctrl){

      scope.type = attrs.type;
      scope.p = attrs.p;
      scope.for = attrs.for;
      scope.check = attrs.check;

      scope.isChecked = function() {
        console.log("checked");
      }
    }
  };
});
app.directive(“checkbox”),函数($compile){
var check=“{check}}”;
如果(检查==true){
变量输入检查=“”;
}否则{
变量输入检查=“”;
}
var temp=''+inputCheck+''+''+'{{p}}';
返回{
限制:“E”,
模板:temp,
作用域:{},
链接:函数(范围、元素、属性、ctrl){
scope.type=attrs.type;
scope.p=attrs.p;
scope.for=attrs.for;
scope.check=attrs.check;
scope.isChecked=函数(){
控制台日志(“已检查”);
}
}
};
});
我怎么能做到

请检查这个

var jimApp=angular.module(“mainApp”,[]);
jimApp.controller('mainCtrl',函数($scope){
})
.指令(“复选框”,函数(){
返回{
限制:“E”,
要求:'ngModel',
作用域:{name:@',ngModel:='},
模板:“{{name}}”
};
});

{{value}}

@SSH是的。但未成功。请提供您的尝试,以便我们检查是否有问题。请分享它可能对您有所帮助。@ssh问题已更新。