<div class="btn-group" align="center">
<button ng-click="myFunction()" id="one" class='btn btn-primary btn1' >Save Entry</button>
<button ng-click="close_window()" id="two" class='btn btn-primary btn2'>Exit</button>
</div>
var app=angular.module('userApp',[])
app.ng-controller('userController', function($scope)){
$scope.myFunction = function(){
var change= document.getElementById("one")
if (change.innerHTML == "Save Entry")
{
change.innerHTML = "Saved!";
}
else
{
change.innerHTML = "Save Entry";
}
return
}
$scope.close_window = function() {
if (confirm("Close Window?")) {
window.close();
}
}
}
这下面的代码工作正常,如果我没有添加任何控制器,只是声明函数像
function myFunction(){
var change= document.getElementById("one")
if (change.innerHTML == "Save Entry")
{
change.innerHTML = "Saved!";
}
else
{
change.innerHTML = "Save Entry";
}
}
function close_window() {
if (confirm("Close Window?")) {
close();
}
但是当我添加控制器时,按钮会停止工作。我尝试了堆栈溢出的每个函数定义和答案,但似乎没有任何工作正常。如何使用按钮在控制器内的工作,就像以前一样工作?
答案 0 :(得分:2)
shoppingListItem.appendChild(this.createDeleteButton());
var app=angular.module('userApp',[])
app.controller('userController', function($scope){
$scope.myFunction = function(){
var change= document.getElementById("one")
if (change.innerHTML == "Save Entry")
{
change.innerHTML = "Saved!";
}
else
{
change.innerHTML = "Save Entry";
}
return
}
$scope.close_window = function() {
if (confirm("Close Window?")) {
window.close();
}
}
});
您正在使用<DOCTYPE html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</head>
<body ng-app="userApp" ng-controller="userController">
<div class="btn-group" align="center">
<button ng-click="myFunction()" id="one" class='btn btn-primary btn1' >Save Entry</button>
<button ng-click="close_window()" id="two" class='btn btn-primary btn2'>Exit</button>
</div>
</body>
它应该是app.ng-controller
。app.controller
是一个指令,用于在视图中定义控制器。
ng-controller