基本上,我只需要在点击链接时将一个id值从一个页面传递到另一个页面。有没有我可以效仿的例子?
答案 0 :(得分:0)
使用服务或工厂。下面,我创建了myfactory
,其中公开了setSomething
和getSomething
方法。由于工厂和服务是单件,因此这些值将在应用程序的生命周期内保留(除非您手动更改它们)。因此,当controller1
执行myfactory.setSomething('hi')
时,任何调用myfactory.getSomething()
的内容都会 hi ,直到其他内容为myfactory.setSomething('something else');
angular
.module('app', [])
.controller('controller1', controller1)
.controller('controller2', controller2)
.factory('myfactory', myfactory);
function controller1(myfactory) {
myfactory.setSomething('hi');
}
function controller2(myfactory) {
var vm = this;
vm.something = myfactory.getSomething(); // hi
}
function myfactory() {
var something;
var setSomething = function(something) {
this.something = something;
}
var getSomething = function() {
return something;
}
return {
setSomething: setSomething,
getSomething: getSomething
}
}