0

Briefly - How to inject markup for JQuery Mobile widgets by way of a directive and have the markup actually convert into a JQM widget.

Plnkr: http://plnkr.co/edit/3AZV93mktxPAPwU4JKgM?p=preview

Given a templateURL within an angular directive, whose target content specifies JQuery Mobile listview markup, how do I ensure that the markup is correctly acted upon by JQuery Mobile so that I get a proper JQM listview widget instead of just styled HTML?

My current attempt is to target the listview within the linking function and call...

$("[data-role='listview']",element).listview();

This does seem to have an effect in that if I call this function my unordered list is is then decorated with JQM css classes, but the unordered list is not widgetized as expected by the listview function.

My Directive

app.directive('testDirective', function () {

    var link=function(scope, element, attrs){
      console.log($("[data-role='listview']"));
      $("[data-role='listview']",element).listview();
    };

    return {
        restrict: 'EA',
        templateUrl : "templates/static.html",
        link: link
    };
});

Template Markup JQM-listivew

<ul data-role="listview" data-inset="true">
    <li ng-repeat="item in data"><a href="#">{{item}}</a></li>
</ul>

What the result should look like...

enter image description here

What the result actually looks like

enter image description here

Final Working Directive...

app.directive('testDirective', function () {

  var link=function(scope, element, attrs){
    console.log(element.context.innerHTML);
    var lview=$("[data-role='listview']",element);
    lview.listview();

    scope.$watchCollection("data",function(){
      console.log("watching collection");
      lview.listview("refresh");
    }); 
 };

 return {
    restrict: 'EA',
    scope:false,
    templateUrl : "templates/static.html",
    link: link
 };

})

mccainz
  • 3,478
  • 5
  • 35
  • 45

1 Answers1

0

You need to run .listview(); after angular has finished rendering.

see this for another example:

AngularJS: How to run additional code after AngularJS has rendered a template?

Community
  • 1
  • 1
Nikos
  • 7,295
  • 7
  • 52
  • 88
  • Is that now what my .listview() call in the linking function is doing? – mccainz Feb 12 '14 at 14:54
  • Left the listview call within the linking function and added a watch function on data which calls .listview('refresh') – mccainz Feb 12 '14 at 15:04
  • 1
    Updated with one more change, needed to use use scope.$watchCollection instead of scope.$watch in order to respond to changes in the data after the initial linking. – mccainz Feb 12 '14 at 16:20