我是AngularJS的新手,我遇到了问题。我正在使用当前两个视图执行SinglePageApplication。在主视图上,我有一个打印“toto”的按钮。但是,当我切换到第二个视图然后回到第一个视图时,它将打印两次(然后三次,四次......对于视图之间的每次切换)。
无法弄清楚为什么因为我似乎只在一个地方叫我的控制器。这是我的代码:
网页模板:
<body ng-app="myApp">
<section id="container" class="">
<% include partials/header %>
<% include partials/aside %>
<section id="main-content">
<section class="wrapper">
<ui-view ></ui-view>
</section>
<footer>
<% include partials/footer %>
</footer>
</section>
</section>
HomeView:
<div class="button">
<span>Button</span>
<div >
<a ng-click="test()" ></a>
</div>
</div>
控制器:
controller('MonitorCtrl', function ($scope, socket, stateService) {
$scope.data = stateService.get(); //init service saving state
$scope.test= function() {
console.log('toto');
};
});
路线:
config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise("/");
$stateProvider
.state("home", {
url: "/",
templateUrl: "partials/home.ejs",
controller: "MonitorCtrl"
})
.state("configuration", {
url:"/config",
templateUrl: "partials/config.ejs",
controller: "ConfigCtrl"
});
}).
服务
factory('stateService', function() {
var data = {} ;
data = "test";
return {
get: function() {
return data;
}
};
});
除了(菜单):
<ul class="sidebar-menu">
<li class="">
<a class="" ui-sref="home">
<i class="icon_desktop"></i>
<span>Monitoring</span>
</a>
</li>
<li>
<a class="" ui-sref="configuration">
<i class="icon_cog"></i>
<span>Configurations</span>
</a>
</li>
如果有人有想法我会很感激。