Benjamin W Benjamin W - 2 months ago 10
AngularJS Question

angularjs get template and pass variable to template

<section ng-app="app" ng-controller="ctrl">
<div id="output">{{ foo }}</div>
<button ng-click="myFun()">Click me</button>
</section>

var app = angular.module("app", []);
app.controller('ctrl', function($scope, $http){
$scope.bar = 123;
$scope.myFun = function(){
$http.get('template.html').then(function(response){
$scope.foo = response.data;
});
};
});


//template

<h1>{{ bar }}</h1>


I'm new in angularjs

I try to create a ng-click and get a template data into page like ajax

I try to use variable inside of template

anyone know how to pass variable to template in angularjs?

Answer

I find another way to solve your question, i hope this sample helps you

 <html ng-app="app" ng-controller="ctrl">
 <head>
<title>sample</title>
</head>
<body>
<div ng-include="template"></div>
<button ng-click="myFun()">Click me</button>

<script src="angular.js"></script>
<script type="text/javascript">
    var app = angular.module("app", []);
    app.controller("ctrl",
        function ($scope) {
            $scope.bar = 123;

            $scope.myFun = function () {
                $scope.template = "temp.html";
            };
        });
</script>

Template

<h1>{{ bar }}</h1>