【发布时间】:2016-05-18 14:43:49
【问题描述】:
我正在尝试为控制器上的角度服务设置一个值并从另一个控制器获取它,但该服务不保存该值。
App.js
var app = angular.module('Test', []);
app.service('editorPost', function() {
this.ID = -1;
this.setID = function(ID) {
this.ID = ID;
};
this.getID = function() {
return this.ID;
};
});
第一个控制器
app.controller('DashboardController', ['$scope','editorPost',
function ($scope, editorPost) {
$scope.loadEditor = function(link)
{
editorPost.setID(link.post.ID);
// Displays the correct ID, obtained from the <a> clicked.
console.log(editorPost.getID());
};
}]);
loadEditor 函数的调用方式如下:
<a href="/editor/editor.html" ng-click="loadEditor(this)">{{ post.webStory }}</a>
console.logging 值后,它会重定向到 editor.html 并显示来自服务的默认 ID 值。
第二个控制器(来自 editor/editor.html 的那个):
app.controller('EditorController', ['$scope','editorPost',
function ($scope,editorPost)
{
$scope.globals = globals;
//Displays the default value, -1.
console.log(editorPost.getID());
});
提前谢谢你。
【问题讨论】:
标签: angularjs service controller