1

please check this Link

here is my question how can I bind a controller to Page2Controller to print some thing in

{{wat}}

1 Answers1

1

The simple way is to use $compile in the controller.

Compiles an HTML string or DOM into a template and produces a template function, which can then be used to link scope and the template together.

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope, $http, $sce, $compile) {
  $scope.wat = 'blablalba';
  $http.get("page2.html").then(function (response) {
    var str = '<div ng-controller="Page2Controller"> {{wat}} <div style="background: red; height: 50px; width: 50px"></div> </div>';
    var cont = $compile(response.data)($scope);
    angular.element(document.querySelector('p')).append(cont);
  });
});

app.controller('Page2Controller', function($scope) {

});
<div ng-app="myApp" ng-controller="myCtrl">
  <p></p>
</div>

http://plnkr.co/edit/cOu69mPhfrvaA1buDUjT?p=preview

The better way is to using a directive like in this answer: Compiling dynamic HTML strings from database

Community
  • 1
  • 1
Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
  • thank you, just a question how can I bind a loaded controller I mean : var str = '
    {{wat}}
    app.controller('Page2Controller', function($scope) { });';
    – user2764027 May 20 '16 at 09:08
  • I'm not sure I understand the question. `I bind a loaded controller`? What do you mean by `bind`? – Mosh Feu May 22 '16 at 08:01