2

I have not been able to find just documentation on what can be done with templates like mustache or twig. All documentation is a bit of this a bit of that but not a clear list of what you can do with the template.

Trying to write a directive that will generate text and a checkbox. The code if a checkbox needs to be checked is in a function but have no idea how to call it from the template:

In the template:

<input type="checkbox" {{ isChecked(tag) }} />

In the directive:

    link : function(scope, iElement, iAttrs, ngModelCtrl) {
        scope.isChecked = function(item){
            console.log("in ischecked",item,this,arguments);
            return "";
        };
    }

[update]

The reason why I want to do this is because on the server I'm using doctrine and my entities can be filled from JSon. So after a user edits/creates an entity and it's related entities it can be posted as JSON where it'll persist (in a MySQL db).

Problem is that when I remove a related entity (for example a tag from an article) then Angular removes this from the article. I want the article to stay but have a clientCommandRemove property of true so when creating an article entity from posted JSON it knows what to do (remove the tag).

The article should look something like this:

{"text":"some text",id:22,
"tags":[
  {"id":6,"clientCommandRemove":true},
  {"id":2,"clientCommandAdd":true},
  {"id":9}]
}

This tells the entity to take article with id 22 and set the text to "some text" (there are more values but since they're not in the json message they won't change). The tag with id 6 needs to be removed, the tag with id 2 needs to be added and tag with id 9 is unchanged so the Entity won't take any action on this.

As mentioned before; it seems that Angularjs just removes sub objects when using something like ng-options with a multi select so the only way for the entity to update on such information is remove all the tags and add those in the posted article.

HMR
  • 37,593
  • 24
  • 91
  • 160
  • I could be wrong but it looks like you're trying to reimplement the `ngChecked` directive. See https://docs.angularjs.org/api/ng/directive/ngChecked – ivarni Jun 29 '14 at 10:27
  • @ivarni Thank you for your reply. I don't think the code to see if it needs to be checked can fit in an expression. It needs to loop over many items. – HMR Jun 29 '14 at 10:34
  • Expressions can be function invocations so you should be able to do `ng-checked="isChecked(tag)"`. – ivarni Jun 29 '14 at 10:42
  • If you're using `ng-repeat` I can't remember off the top of my head if it creates an isolated scope or not, but if it does you're probably looking at `ng-checked="$parent.isChecked(tag)"` – ivarni Jun 29 '14 at 10:44
  • @ivarni According to this post my code should work: http://stackoverflow.com/a/12466994/1641941 maybe because it's in a directive I should not add the method to scope in the link function. Seems that ng-click isn't doing anything either so maybe have to add the functions somewhere else. – HMR Jun 29 '14 at 10:44
  • 1
    There's a difference between binding inside and outside tags. If you inspect source on this plunker you'll see the function in the second `p` tag was not applied. http://plnkr.co/edit/ZgqYrWUavUFG0LBxYnDX?p=preview – ivarni Jun 29 '14 at 10:48
  • @ivarni Nice, now we know what's broken but need to find a way to fix it. I can do `class="{{isChecked()}}"` and that will call isChecked 9 times even though there are only 3 items. Maybe I can set a class and then check them based on the class. – HMR Jun 29 '14 at 10:58
  • 1
    you'll probably want `ng-class` if you want to go down that route. It's probably being called multiple times because of angular's dirty checking. The framework is calling it repeatedly every time something on the scope changes to see if it affected that function in any way. – ivarni Jun 29 '14 at 11:00

0 Answers0