按下按钮未记录任何AngularJS



我在按下"创建分数"按钮时遇到了问题,计划将其添加到MySQL数据库中,但我是AngularJS的新手,似乎无法点击按钮进行注册——控制台中没有显示任何内容。

非常感谢任何帮助

Html代码:

<div ng-app="addScores" ng-controller="add_new_score">
<label>Match_ID:</label>
<input type="text" ng-model="match_ID" required>
<label>Home_Score:</label>
<input type="text" ng-model="Home_Score" required>
<label>Away_Score:</label>
<input type="text" ng-model="Away_Score" required>
<button ng-click="create_score()">Create score</button>
</div>
<script>
var scoreApp = angular.module('addScores', []);
scoreApp.controller('add_new_score', function($scope, $http) {
$scope.create_score = function () {
console.log("logged");
var scoreData = new scoreObj($scope.match_ID, $scope.Home_Score, $scope.Away_Score);
$http(
{
method: 'POST',
url: "/addScore",
data: scoreData
}
).then(function successCallback(response) {
alert("Success");
}, function errorCallback(response) {
alert("Sorry, there was a problem!");
}
)
};

function scoreObj(match_ID, Home_Score, Away_Score) {
this.match_ID = Match_ID;
this.Home_Score = Home_Score;
this.Away_Score = Away_Score;
}
})

页面中是否包含angular.js引用?

你的代码运行良好

var scoreApp = angular.module('addScores', []);
scoreApp.controller('add_new_score', function($scope, $http) {
$scope.create_score = function () {
console.log("It works");
var scoreData = new scoreObj($scope.match_ID, $scope.Home_Score, $scope.Away_Score);

};
function scoreObj(match_ID, Home_Score, Away_Score) {
this.match_ID = match_ID;
this.Home_Score = Home_Score;
this.Away_Score = Away_Score;
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.4/angular.min.js"></script>
<div ng-app="addScores" ng-controller="add_new_score">
<label>Match_ID:</label>
<input type="text" ng-model="match_ID" required>
<label>Home_Score:</label>
<input type="text" ng-model="Home_Score" required>
<label>Away_Score:</label>
<input type="text" ng-model="Away_Score" required>
<button ng-click="create_score()">Create score</button>
</div>

最新更新