3

How do I instantiate the Controller so that its methods may work?

I am loading via AJAX a SettingsController, but once it is loaded, its methods are not callable.

What do I have to do to instantiate this code?

I looked at $compile, but that doesn't seem to work.

Standard way of using twitter bootstrap to load partial

$("#modal").modal({remote: 'partials/users/settings.html'})

The partial that is loaded :

%div{'ng-controller' => 'SettingsController'}
   = form_tag '', 'ng-submit' => 'update_settings($event)', :method => :post do |f|

In my SettingsController :

$scope.update_settings = ($event) ->
  alert 'hey'

Doesn't do anything.

7
  • 2
    You have the strangest questions about Angular, lol. Out of curiosity, why are you attempting this? Commented Feb 7, 2013 at 19:53
  • 2
    Haha sorry, I guess I coded myself stupid again. Its a settings modal that I loaded. But when I load it it doesn't work. Commented Feb 7, 2013 at 19:56
  • 2
    +1 for "coded myself stupid". I'll remember that for next time I'm in the same state. Commented Feb 7, 2013 at 19:58
  • Try posting your AJAX code. The answer may well lie within. Commented Feb 7, 2013 at 20:00
  • 1
    That makes more sense... You made it sound like you were trying to download a string of JavaScript and register it as a controller... which would be damn near impossible outside of the configuration phase. Commented Feb 7, 2013 at 20:12

1 Answer 1

5

Okay it looks like the issue here is you want to dynamically load some HTML into a modal. I'm not sure what you're using for your modal plugin, but you're going to need to do something like this:

<div id="myModal" ng-include="source"></div>

Where source is a property on your $scope:

$scope.source = 'test.html';

You could then listen for the event $includeContentLoaded in your directive, and call your modal function:

scope.$on('$includeContentLoaded', function () {
    $('#myModal').modal();
});

Have angular handle pulling down the partial you want to include... then open it with your modal.

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

4 Comments

This doesn't look reusable though. How would you make it so that you could dynamically load content into a single div that could represent all modals?
Another, native AngularJS alternative (without including jQuery) is to use the $dialog service from angular-ui.github.com/bootstrap
@Trip: To dynamically load the content, you would change the value you're passing into ng-include. I've updated my answer a bit, have a look.
There are 1000 ways to skin this cat, but the crux of it is that you should probably be letting ng-include handle loading the partial for you.

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.