我正在尝试包含一个模板文件 views/infowindow.html作为我为启动 google maps api 而编写的服务中 InfoWindow 的内容:
for ( var count = locations.length, i = 0; i < count; i++ ) {
var latLng = locations[i],
marker = new google.maps.Marker({
…
}),
infowindow = new google.maps.InfoWindow();
google.maps.event.addListener(
marker,
'click',
(function( marker , latLng ){
return function(){
var content = '<div ng-include src="\'infowindow.html\'"></div>';
infowindow.setContent( content );
infowindow.open( Map , marker );
}//return fn()
})( marker , latLng )
);//addListener
}//for
但是,Angular 似乎没有处理content当它被插入到 InfoWindow 中时(当通过开发工具检查代码时,插入的代码是 <div ng-include src="'views/infowindow.html'"></div> )。
我希望 Angular 在将我的包含插入到 InfoWindow 之前对其进行预处理,但遗憾的是没有。
我想做的事情可行吗?
我在想,在将模板传递给 infowindow.setContent() 之前,我必须以某种方式缓存模板。 ,但我不知道该怎么做(或者这是否是我应该做的)。我更愿意在事件上加载模板,而不是为每个标记缓存和注入(inject)模板。
编辑 查看 $templateCache 和 related SO question .
EDIT 2 这是一个 plunk试图使用 $compile (InfoWindow的内容还是<div id="infowindow_content" ng-include src="'infowindow.html'"></div>)
这方面的基础来自Mark's answer below .在他的解决方案中,InfoWindow 的内容是在第一次点击(任何标记)时编译的,但 InfoWindow 直到再次点击任何标记才真正打开,这可能是因为 GoogleMaps 不耐烦。
移动 $compile外部,然后将编译后的模板传递给 .addListener解决了这个问题:
for ( … ) {
…
infowindow = new google.maps.InfoWindow();
scope.markers …
var content = '<div id="infowindow_content" ng-include src="\'infowindow.html\'"></div>';
var compiled = $compile(content)(scope);
google.maps.event.addListener(
marker,
'click',
(function( marker , scope, compiled , localLatLng ){
return function(){
scope.latLng = localLatLng;//to make data available to template
scope.$apply();//must be inside write new values for each marker
infowindow.setContent( compiled[0].innerHTML );
infowindow.open( Map , marker );
};//return fn()
})( marker , scope, compiled , scope.markers[i].locations )
);//addListener
}//for
最佳答案
将 content 添加到 DOM 后,您需要找到它(可能使用 jQquery 选择器?),然后 $compile() 并应用它它到适当的范围。这将导致 Angular 解析您的内容并根据它找到的任何指令(如 ng-include)采取行动。
例如,$compile(foundElement)(scope)
没有更多代码,很难给出更准确的答案。但是,这里有一个 similar question and answer你可以看看。
更新:好吧,我终于让它工作了,我学到了一些东西。
google.maps.event.addListener(
marker,
'click',
(function( marker , scope, localLatLng ){
return function(){
var content = '<div id="infowindow_content" ng-include src="\'infowindow.html\'"></div>';
scope.latLng = localLatLng;
var compiled = $compile(content)(scope);
scope.$apply();
infowindow.setContent( compiled[0].innerHTML );
infowindow.open( Map , marker );
};//return fn()
})( marker , scope, scope.markers[i].locations )
我的印象是只有 DOM 元素可以被 $compiled —— 也就是说,我首先必须将内容添加到 DOM,然后再编译它。事实证明那不是真的。在上面,我首先针对 scope 编译 content,然后将其添加到 DOM。 (我不知道这是否会破坏数据绑定(bind)——即由 $compile 设置的 $watch()es。)我必须设置 scope.latLng 因为 ng-included 模板需要插入 {{latLng[0]}} 和 {{latLng[1]}}。我使用 innerHTML 而不是 outerHTML 以便只插入 infowindow.html 的内容。
Plunker .
更新 2: 第一次点击不起作用。似乎直到第二次单击才加载“infowindow.html”(我尝试第二次调用 scope.$apply() ......没有帮助)。当我让 plunker 工作时,我在 index.html 中内联了 infowindow.html 的内容:
<script type="text/ng-template" id="/test.html">
<h4>{{latLng[0]}},{{latLng[1]}}</h4>
</script>
我在 addListener() 中使用了它:
var content = '<div id="infowindow_content" ng-include src="\'/test.html\'"></div>';
我更改了 plunker 以使用内联模板。
关于javascript - AngularJS ng-include 在 Google Maps InfoWindow 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14226975/
转自:spring.profiles.active和spring.profiles.include的使用及区别说明下文笔者讲述spring.profiles.active和spring.profiles.include的区别简介说明,如下所示我们都知道,在日常开发中,开发|测试|生产环境都拥有不同的配置信息如:jdbc地址、ip、端口等此时为了避免每次都修改全部信息,我们则可以采用以上的属性处理此类异常spring.profiles.active属性例:配置文件,可使用以下方式定义application-${profile}.properties开发环境配置文件:application-dev
我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的
我正在使用Rails3.2.3和Ruby1.9.3p0。我发现我经常需要确定某个字符串是否出现在选项列表中。看来我可以使用Ruby数组.includemethod:或正则表达式equals-tildematchshorthand用竖线分隔选项:就性能而言,一个比另一个好吗?还有更好的方法吗? 最佳答案 总结:Array#include?包含String元素,在接受和拒绝输入时均胜出,对于您的示例只有三个可接受的值。对于要检查的更大的集合,看起来Set#include?和String元素可能会获胜。如何测试我们应该根据经验对此进行测试
我有这个:AccountSummary我想单击该链接,但在使用link_to时出现错误。我试过:bot.click(page.link_with(:href=>/menu_home/))bot.click(page.link_with(:class=>'top_level_active'))bot.click(page.link_with(:href=>/AccountSummary/))我得到的错误是:NoMethodError:nil:NilClass的未定义方法“[]” 最佳答案 那是一个javascript链接。Mechan
我有以下代码,其中有一个小错误,case语句返回值“other”,即使第一个“when”语句评估为true并且应该返回“boats”。我已经关注这个很久了,一定是个小东西。CATEGORY_CLASSES={:boats=>[1,2,3,4,5,6],:houses=>[7,8,9,10],:other=>[11,12,13,14,15,16]}category_id=1category=casecategory_idwhenCATEGORY_CLASSES[:boats].include?(category_id);"boats"whenCATEGORY_CLASSES[:house
我看到有关未找到文件min.map的错误消息:GETjQuery'sjquery-1.10.2.min.mapistriggeringa404(NotFound)截图这是从哪里来的? 最佳答案 如果ChromeDevTools报告.map文件的404(可能是jquery-1.10.2.min.map、jquery.min.map或jquery-2.0.3.min.map,但任何事情都可能发生)首先要知道的是,这仅在使用DevTools时才会请求。您的用户不会遇到此404。现在您可以修复此问题或禁用sourcemap功能。修复:获取文
我使用以下钩子(Hook)来检查在执行includeFoo时执行包含的模块:moduleFoodefself.included(includer)putsincluderendendModule#include在模块中(通常使用它的地方)与在顶层的行为不同。在模块内部,self是模块,它是Module的一个实例.当我调用include,执行包含的模块是whatself是:moduleBarputsself#=>BarincludeFoo#=>includer:Barend在ruby脚本的顶层,self是main,它是Object的一个实例.当我调用include在顶层,包含的模块是
我有一个用Rails3编写的站点。我的帖子模型有一个名为“内容”的文本列。在帖子面板中,html表单使用tinymce将“content”列设置为textarea字段。在首页,因为使用了tinymce,post.html.erb的代码需要用这样的原始方法来实现。.好的,现在如果我关闭浏览器javascript,这个文本区域可以在没有tinymce的情况下输入,也许用户会输入任何xss,比如alert('xss');.我的前台会显示那个警告框。我尝试sanitize(@post.content)在posts_controller中,但sanitize方法将相互过滤tinymce样式。例如
出于某种原因,我必须为Firefox禁用javascript(手动,我们按照提到的步骤执行http://support.mozilla.org/en-US/kb/javascript-settings-for-interactive-web-pages#w_enabling-and-disabling-javascript)。使用Ruby的SeleniumWebDriver如何实现这一点? 最佳答案 是的,这是可能的。而是另一种方式。您首先需要查看链接Selenium::WebDriver::Firefox::Profile#[]=
我是Ruby和Watir-Webdriver的新手。我有一套用VBScript编写的站点自动化程序,我想将其转换为Ruby/Watir,因为我现在必须支持Firefox。我发现我真的很喜欢Ruby,而且我正在研究Watir,但我已经花了一周时间试图让Webdriver显示我的登录屏幕。该站点以带有“我同意”区域的“警告屏幕”开头。用户点击我同意并显示登录屏幕。我需要单击该区域以显示登录屏幕(这是同一页面,实际上是一个表单,只是隐藏了)。我整天都在用VBScript这样做:objExplorer.Document.GetElementsByTagName("area")(0).click