0

i dont want to load controller when page is load.i want to load it after click the button. Here is my code, help me!!

    <!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<button onclick="myfunc()"></button>
Name: <input ng-model="name">
</div>
<script>
function myfunc(){
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.name = "John Doe";
});
}
</script>
<p>Use the ng-model directive to bind the value of the input field to a property made in the controller.</p>
</body>
</html>
2
  • 4
    Possible duplicate of AngularJS: lazy loading controllers and content Commented Aug 24, 2018 at 3:38
  • The suggested duplicate is not the best way to solve this problem. The user should use the ng-click directive to invoke a function that loads the ng-model. Commented Aug 26, 2018 at 11:19

1 Answer 1

0

Instead of using onclick to load a controller, use the ng-click directive to invoke a function that loads the ng-model.

angular.module("myApp",[])
.controller('myCtrl', function($scope) {
    $scope.myfunc = function() {
        $scope.name = "John Doe";
    };
});
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
        <button ng-click="myfunc()">Load</button>
        Name: <input ng-model="name">
        <br>name={{name}}
</body>

Sign up to request clarification or add additional context in comments.

Comments

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.