如何分享angularjs两个控制器之间的数据 [英] how to share data between two controllers in angularjs
问题描述
荫试图共享由$ HTTP返回到另一个控制器的数据。
Iam trying to share the data returned by $http to another controller.
$scope.getClickData=function(dataId){
var postData={"containerInstanceId" : "3842a251-1708-4df0-b941-db27a03d91ab","fetchMetadata":true,"parameters":{"#{organizationTypeID}":"abf1c91f-fea8-4033-a731-136c339525c7"}};
$http.post('http://latest.tapplent.info/api/app/layouts/v1/container-instance/data',postData).
success(function(data, status, headers, config) {
$scope.data=data;
console.log(data.containerData[0].propertyData.NULL[0].organizationTypeName.value);
}).
error(function(data, status, headers, config) {
console.log("error");
});
}
});
app.controller('webView', ['$scope', function($scope) {
console.log($scope.data);
}]),
我怎样才能获得的数据网页视图控制器。请帮我朋友,我怎么能解决这个问题。
How can i get data to webview controller. Please help me friends how can i solve this problem.
先谢谢了。
推荐答案
通过三种方式。
1。)您可以使用角工厂/服务。
1.) You can use Angular factory/services.
myApp.factory('unicornLauncher', ["apiToken", function(apiToken) {
return new UnicornLauncher(apiToken);
}]);
https://docs.angularjs.org/guide/providers
2。)通过使用广播,放出的。
2.) By use of broadcast and Emit.
您可以共享通过 $广播
或 $的Emit
数据,然后通过<$赶上任何控制器此数据C $ C> $关于。
You can share data via $broadcast
or $Emit
and then catch this data in any controller via $on
.
https://docs.angularjs.org/api/ng/type/ $ rootScope.Scope
https://docs.angularjs.org/api/ng/type/$rootScope.Scope
我preFER角工厂/多播服务/放出由于性能。
I prefer Angular factory/services over broadcast/emit due to performance.
3)。或者你可以用$ rootscope。但是,这是作为pferred至少rootscope $ P $是全局范围。你不应该垃圾全局范围。
3.) Or you can use $rootscope. But that is least preferred as rootscope is your global scope. You should not litter your global scope.
这篇关于如何分享angularjs两个控制器之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!