0

Basically in my controller, I'm doing an $http.get() to load some html to set a "current view". My issue is that I can't figure out how to rebind the the jQuery event with this new dynamic content.

Right now, I've resorted to somethign like this:

$http.get('/someurl', {}).success(function(data){
  $scope.detailedView = data;
  // Now here comes the rebinding
  setTimeout(function(){
    // Use jquery to bind the new content
  }, 1500);
});

I've been looking for a solution, and anything related that I've found points to using a directive. I've looked into this, but I do not know how a directive would be used for something like this.

Note without the timeout, the bindings run before the dynamic content is actually in the DOM. I've also tried finding something that would be similar to hooking into something after the $apply would run but have not found anything similar.

3
  • What jQuery are you trying to use? What do you need to do? Commented Apr 8, 2013 at 23:22
  • I'm just trying to use draggable/droppable from jquery-ui, I want send a post request when someone is done dragging and use the response back in my ng-app Commented Apr 9, 2013 at 0:15
  • You'll need to separate your logic. UI stuff goes in a directive - not in a controller - and your callback logic with the $http in a controller or service. The directive lifecycle will ensure that your timeout shouldn't be needed. If it is, Charlie's answer below will work. Commented Apr 9, 2013 at 0:21

1 Answer 1

2

First should see if what you are doing with jQuery can't be done using angular.

Here's the most simplistic version of a directive that can be used:

<div ng-repeat="item in items" my-directive>Item {{$index+1}}</div>
app.directive('myDirective', function ($timeout) {
    return function (scope, element, attrs) {
        $timeout(function () {
            /* element is a jQuery object when jQuery loaded in page before angular,*/
            /* otherwise is a jQlite object that has many of same methods as jQuery*/
            element.css({ 'border': '1px solid green', 'margin': '10px 30px','padding':'5px'});
        }, 0);
    }
 });

Here's a demo that uses a long timeout to generate data for repeated items that use the directive:

http://jsfiddle.net/AXYGL/

Sign up to request clarification or add additional context in comments.

3 Comments

it seems a little.. inelegant, to have to rely on a timeout for the ajax request to finish, but it does do the trick.
simply one of the issues with angular and the digest cycle. Directives can run several times within a cycle and element doesn't exist each time if it is being dynamically generated. The timeout in directive has nothing to do with ajax delay
I ended up tweaking some things to make it more angular and then used something like this but without the $timeout and it worked. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.