Z
Z
zagamay_ru2015-09-29 21:28:07
JavaScript
zagamay_ru, 2015-09-29 21:28:07

How to initialize controller in angularjs after Ajax?

I have a question about angularJs.

I have one controller which is responsible for loading templates. The essence of this controller is to call a function when the url (history api) changes, the function itself makes an Ajax request to the server and returns the HTML code of the page in which its controller is connected to process the document, something like this

$http.post($location.url(), xx).then(function( result ) {
       $scope.myTemplate = $sce.trustAsHtml( result.data );
}, function( result ) {
      console.log( result );
})


Got the template in $scope.myTemplateand displayed it on the page Everything is great! But how can I now initialize the controller that belongs to this template in this template (myTemplate)? In this piece, I connected the controller to the very bottom, registered it in ng-controller and nothing happened. Help me to understand. <div ng-bind-html="myTemplate"></div>


Answer the question

In order to leave comments, you need to log in

2 answer(s)
_
_ _, 2015-09-29
@AMar4enko

$scope.templateUrl = $location.url();
ng-bind-html is not designed to compile and bind a template (if you know what I mean)

A
Alex, 2015-09-30
@streetflush

Maybe I'm wrong of course, but here you need $routeProvider + ngView plnkr.co/edit/BYExpQyDQdnCFP3ZH5uM?p=preview
And what are you doing: a bicycle. =)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question