6

I use semantic-ui in my project, the pulgin is checkbox
someone say if use jQ plugin, you must use it in angular directive
but it doesn't work

the checkbox of semantic-ui setting in semantic-ui API document, you must set this to init checkbox

$('.ui.checkbox').checkbox();

I try to change it to angular like this:

app.html

<div class="ui animate list">
  <div class="item ui toggle checkbox" todo-checkbox ng-repeat="item in day track by $index">
    <input type="checkbox">
    <label ng-bind="item.content"></label>
  </div>
</div>

and this is directive in angularjs file

todoApp.directive('todoCheckbox', function() {
  return {
    restrict: 'A',
    link: function(scope, elem, attrs) {
      $(elem).checkbox();
    }
  };
});

but it doesn't work in my project. Why?

1
  • @StanislavKralin your edits would be better received if you explained, in your edit summary, why you're removing the tag. It's obvious from the diff that you are remove the tag. Commented Jun 14, 2017 at 22:07

3 Answers 3

1

You're close. elem is the element of the directive. In this case it is

<div class="item ui toggle checkbox" todo-checkbox ng-repeat="item in day track by $index">
  <input type="checkbox">
  <label ng-bind="item.content"></label>
</div>

Now, if we use find to help us locate the input field within the elem, then we can select the input field and run the checkbox method.

todoApp.directive('todoCheckbox', function() {
  return {
    restrict: 'A',
    link: function(scope, elem, attrs) {
      angular.forEach(elem.find( "input" ), function(inputField) {
        inputField.checkbox();
      }
    }
  };
});
Sign up to request clarification or add additional context in comments.

6 Comments

Your message was cut-off. Can you post again? I'm happy to help solve your problem.
It doesn't work with your method, in officlal API, the checkbox() is bind on .ui.checkbox element, I think the problem is not which elem to bind
I made a slight adjustment to my code. Try it out. Adjustment below. elem.find( "input" )[0].checkbox();
after the adjustment will an error code,TypeError: elem.find(...)[0].checkbox is not a function
I try this, if I don't use ng-repeat, elem.checkbox() can work normal,but if I use ng-repeat,it can't work,I think this is the main reason
|
0

A bit late to the party but you should be able to just move the todo-checkbox to the input tag (same with the semantic ui specific attributes)

Comments

0

Try

angular.element(elem).checkbox();

instead of

$(elem).checkbox();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.