I am trying to load a view with custom message in it. So here is the scenario
我正在尝试加载包含自定义消息的视图。所以这是场景
Let say I am on index and I am going to navigate to index2.html.
假设我在索引上,我将导航到index2.html。
Index2.html will render the custom message passed to it. One way is definitely to do it with $routeParams but I don't what to pass the information through the url, rather I want it in a general POST format where passed data is not exposed in url
Index2.html将呈现传递给它的自定义消息。一种方法肯定是使用$ routeParams,但我不知道通过url传递信息,而是我希望它采用一般的POST格式,其中传递的数据不会在url中公开
Also I don't want to use localstorage, session or cookie. I want it in a way where I pass the data to the view and it should get rendered.
另外,我不想使用localstorage,session或cookie。我希望它以我将数据传递给视图的方式进行渲染。
If anyone can help me with this
如果有人可以帮我这个
1 个解决方案
#1
4
FACTORY - The right way to share data between pages is a factory/service.
工厂 - 在页面之间共享数据的正确方法是工厂/服务。
In this example I'll show you how to share data between two controllers, But it's the same thing. Once you invoke the service you have access to his instance and his data.
在这个例子中,我将向您展示如何在两个控制器之间共享数据,但这是一回事。一旦您调用该服务,您就可以访问他的实例和他的数据。
var myApp = angular.module('myApp', []);
myApp.factory('shareService', function(){
return {
title: "some title"
};
});
myApp.controller('controller1', function($scope, shareService){
var ctrl1 = this;
ctrl1.title = shareService.title;
});
myApp.controller('controller2', function($scope, shareService){
var ctrl2 = this;
ctrl2.title = shareService.title;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="controller1 as ctrl1">
{{ctrl1.title}}
</div>
<div ng-controller="controller2 as ctrl2">
{{ctrl2.title}}
</div>
</div>
Read more about services on AngularJs doc
阅读有关AngularJs doc上的服务的更多信息
UPDATE
UPDATE
A working example for routing and shared data on plunkr
在plunkr上路由和共享数据的工作示例
#1
4
FACTORY - The right way to share data between pages is a factory/service.
工厂 - 在页面之间共享数据的正确方法是工厂/服务。
In this example I'll show you how to share data between two controllers, But it's the same thing. Once you invoke the service you have access to his instance and his data.
在这个例子中,我将向您展示如何在两个控制器之间共享数据,但这是一回事。一旦您调用该服务,您就可以访问他的实例和他的数据。
var myApp = angular.module('myApp', []);
myApp.factory('shareService', function(){
return {
title: "some title"
};
});
myApp.controller('controller1', function($scope, shareService){
var ctrl1 = this;
ctrl1.title = shareService.title;
});
myApp.controller('controller2', function($scope, shareService){
var ctrl2 = this;
ctrl2.title = shareService.title;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="controller1 as ctrl1">
{{ctrl1.title}}
</div>
<div ng-controller="controller2 as ctrl2">
{{ctrl2.title}}
</div>
</div>
Read more about services on AngularJs doc
阅读有关AngularJs doc上的服务的更多信息
UPDATE
UPDATE
A working example for routing and shared data on plunkr
在plunkr上路由和共享数据的工作示例