我只在控制器中调用方法console.log(data)
一次,但控制台中的输出显示两次。
这是控制器:
taskAppControllers.controller('ToDoCtrl', ['$scope',
function($scope){
$scope.loadToDoItems = function(){
var data = ls.get("toDoData");
if (data == null) data = [];
console.log(data);
}
$scope.toDoItems = $scope.loadToDoItems();
$scope.addToDoItem(){};
}]);
这就是观点:
<div ng-controller="ToDoCtrl">
<form>
<input type="text" ng-model="toDoItem">
<input type="submit" ng-click="addToDoItem()">
</form>
</div>
路由器:
var TaskApp = angular.module('TaskApp', [
'ngRoute',
'taskAppControllers'
]);
TaskApp.config(['$routeProvider',
function($routeProvider){
$routeProvider.
when('/home', {
templateUrl: "partials/home.html",
controller: "HomeCtrl",
title: 'Home',
icon: 'ion-android-menu',
link: '#/main-menu'
}).
when('/main-menu', {
templateUrl: "partials/main-menu.html",
controller: "MainMenuCtrl",
title: 'Main menu',
icon: 'ion-close',
link: '#/'
}).
when('/todo', {
templateUrl: "partials/todo.html",
controller: "ToDoCtrl",
title: 'To do',
icon: 'ion-close',
link: '#/main-menu'
}).
otherwise({
redirectTo: "/home"
});
}]);
答案 0 :(得分:2)