angularjs - Attach Controller to $http html data? -


what want load separated templates , append controller each one:

an approach:

$http({     method: 'get',     url: 'templates/mytemplate.html',     controller:'myctrl' })  function myctrl($scope){     $scope.var1= "scoped variable"; } 

mytemplate.html:

tag

{{var1}}

that aproach question: loading angularjs controller dynamically

it appears scenario place apply ng-include. example, given markup:

<div ng-controller="mainctrl">     <div ng-include="template"/> </div> 

and code in mainctrl:

function mainctrl($scope) {   // logic determine template want load   $scope.template = 'templates/mytemplate.html'; } 

and code in templates/mytemplate.html:

<div ng-controller="templatectrl">      <!-- template content --> </div> 

angular automatically download templates/mytemplate.html , apply templatectrl template. (of course you'd need have templatectrl defined.) when want switch templates, in mainctrl you'll need change value of $scope.template template url; template specify ng-controller attribute indicates appropriate controller template.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -