我已經在控制器中創建了一個函數和一條警報消息,並在頁面中調用它。 頁面加載消息警報兩次。Angular的服務是什麼?我如何使用?
角度控制器運行兩次?
- app.js 在那裏我有添加的模塊和js並調用它的index.html的。
angular.module('flapperNews', [])
.controller('MainCtrl', [
'$scope',
function($scope){
$scope.test = 'Hello world!';
$scope.posts = [
{title: 'post 1', upvotes: 5},
{title: 'post 2', upvotes: 2},
{title: 'post 3', upvotes: 15},
{title: 'post 4', upvotes: 9},
{title: 'post 5', upvotes: 4}
];
$scope.addPost=function(){
if(!$scope.title ||$scope.title==='')return;
$scope.posts.push({title: $scope.title
, upvotes: 0});
$scope.title='';
}
$scope.incrementUpvotes = function() {
//post.upvotes += 1;
alert('dfsakjlkdsjflkdsaj');
};
}])
<html>
<head>
<title> News</title>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js"></script>
<script src="app.js"></script>
<style> .glyphicon-thumbs-up { cursor:pointer } </style>
</head>
<body ng-app="flapperNews" ng-controller="MainCtrl">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="page-header">
<h1>Flapper News</h1>
</div>
{{incrementUpvotes()}}
</div>
</div>
</body>
</html>
也許你可以點擊[編輯]並將相關部分的代碼添加到問題中? – nnnnnn
哪裏是代碼?我們如何知道沒有代碼? – harishr
你使用$ routeProvider嗎?它可能會在模板被渲染兩次時生效。 – SET