[英]update $rootScope.$broadcast
How can I update $rootScope.$broadcast with new value? 如何用新值更新$ rootScope。$ broadcast?
Consider this code: 考虑以下代码:
var test = "fisk";
if(angular.element(this).hasClass('monster')) {
var monster_info = angular.element(this).find("img").attr("title");
$http.get("lib/terrain.php", {params: { monster_data:monster_info}}).success(function(data) {
console.log(data);
$rootScope.$broadcast('mapInfo', data);
});
} else {
$rootScope.$broadcast('mapInfo',test);
}
Here is my controller: 这是我的控制器:
$scope.$on('mapInfo', function(event, mapInfo) {
$scope.mapInfo = mapInfo;
console.log($scope.mapInfo);
});
Here is my html: 这是我的html:
<div ng-bind-html="safeHtml(mapInfo)"></div>
This almost works. 这几乎可行。 When I make the ajax-call, the mapInfo is updated and printed out at the page with the data.
当我进行Ajax调用时,mapInfo会更新并打印到包含数据的页面上。 But when I'm not make the ajax-call, the value of fish is not printed out at the page.
但是,当我不进行ajax调用时,fish的值不会在页面上打印出来。 I can see that the $rootScope is being updated by the value fisk: console.log($scope.mapInfo), but it's not printed out.
我可以看到$ rootScope正在由值fisk更新:console.log($ scope.mapInfo),但未打印出来。
This is a simplified version of your code 这是代码的简化版本
`http://jsfiddle.net/HB7LU/5693/`
it can print out mapInfo 它可以打印出mapInfo
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.