簡體   English   中英

我可以將angularjs控制器放入功能中,然后通過按鈕onclick事件加載它嗎

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM