我们正在使用角度来为我们的应用做类似/不同的行为。
html代码
<html>
<body ng-app="myApp">
<div ng-controller="newsCntrl" >
<form ng-submit="post()" name="postForm" id="postForm" >
<input type="submit" id="submit" value="Submit"/>
</form>
<a href="#" ng-click="likePost(post.id);">Like</a></span>
</div>
</body>
</html>
控制器代码
myApp.controller("newsFeedCtrl", function ($scope, $http) {
console.log("inside newsFeedCtrl");
$scope.post() = function() {
console.log("posting");
}
$scope.likePost = function(postId) {
console.log("liking post");
}
});
问题是当我们点击时;它还调用了不应该发生的post函数。
我们在这里遗漏了什么吗?
答案 0 :(得分:2)
HTML中的控制器名称不匹配:
<div ng-controller="newsCntrl" >
和JS中的控制器:
myApp.controller("newsFeedCtrl", function ($scope, $http)
如果我修复它,它似乎工作。 请检查此fiddle
答案 1 :(得分:1)
您应该删除语法错误
html代码:
<html>
<body ng-app="myApp">
<div ng-controller="newsCntrl">
<form ng-submit="submitPost()" name="postForm" id="postForm">
<input type="submit" id="submit" value="Submit"/>
</form>
<a href="#" ng-click="likePost(post.id);">Like</a>
</div>
</body>
</html>
控制器代码:
myApp.controller("newsFeedCtrl", function ($scope, $http) {
console.log("inside newsFeedCtrl");
$scope.submitPost = function() {
console.log("posting");
}
$scope.likePost = function(postId) {
console.log("liking post");
}
});