1

I have a minimal plunk of this here.

Here's what's happening:

  1. initial $http request is made successfully
  2. a click event is bound to a button in a directive
  3. clicking the button fires the desired function
  4. the $http request in that function (identical to the request in step 1) does not fire

Because the code is short, I'll post it here, too.

template

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <title>AngularJS Plunker</title>
    <!-- angular source -->
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <p>Click this button and an http request should log to the console</p>
    <button make-request act='flip()'>Get Gaius</button>
  </body>

</html>

controller

app = angular.module('plunker', [])

app.controller 'MainCtrl', ($scope, $http) ->
  # this function is just here to show that no errors are thrown
  err = (err) -> console.log 'err', err

  # this successfully gets
  $http.get('gaius.json')
    .then ((res) -> console.log 'init data', res.data), err


  $scope.flip = ->
    # although this function is called,
    console.log 'called to act'
    # http does not get. No request is made.
    $http.get('gaius.json')
      .then ((res) -> console.log 'flip data', res.data), err

app.directive 'makeRequest', ($compile) ->
  scope:
    act: '&'

  link: (scope, element, attrs) ->
    element.bind 'click', (e) -> scope.act()

data

{
  "name": "gaius baltar"
}

Any idea why that request doesn't execute?

1
  • I was encountered this issue few weeks ago and the solution was to call $digest. Alas, this does not work in your case. Commented Jul 10, 2013 at 12:32

1 Answer 1

3

You have to propagate promise resolution, by calling $apply() on the scope.

app.directive 'makeRequest', ($compile) ->
  scope:
    act: '&'

  link: (scope, element, attrs) ->
    element.bind 'click', (e) -> scope.act(); scope.$apply();
Sign up to request clarification or add additional context in comments.

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.