![](/img/trans.png)
[英]How can I use a button onchange event to run the selected button's onclick function?
[英]can i put angularjs controller into function then load it by button onclick event
我不想在頁面加載時加載控制器。我想在單擊按鈕后加載它。 這是我的代碼,幫幫我!!
<!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>
可以使用ng-click
指令來調用加載ng-model
的函數,而不是使用onclick
來加載控制器。
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.