1
<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?

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

can you please explain why? and what if I want to append data, not replace
because in angular we use [directives, services, factories and filter] to handle this situations. please read more about [ngInclude] i'm sure you can find the answer to complete your project. in angular you don't need to handle your codes as jQuery. NOTE: you can use nested ngInclude too.

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.