如何在$ scope style

时间:2016-12-15 21:22:46

标签: ionic-framework angularjs-scope

我正在将google地图应用到我的离子应用中,我的index.html中有一个脚本,只允许地图在index.html中运行。


&#xA ;

但我需要在我的模板文件route.html中使用我的地图,所以我相信我应该将index.html中的脚本移动到特定的controller.js文件中,但这里的内容是用$ scope风格编写的,可以有谁告诉我怎样才能将样式写入$ scope样式?





为什么实际情况在route.html中不起作用,因为使用相同的代码?< / p>&#xA;&#xA;

 &lt; div id =“map”&gt;&lt; / div&gt; &#xA;  
&#xA;&#xA;

这是我在index.html中的脚本:

&#xA;&#xA;
  &LT;脚本&GT;&#XA;函数initMap(){&#xA; var uluru = {lat:-25.363,lng:131.044};&#xA; var map = new google.maps.Map(document.getElementById('map'),{&#xA; zoom:4,&#xA; center:uluru&#xA;});&#xA; var marker = new google.maps.Marker({&#xA; position:uluru,&#xA; map:map&#xA;});&#xA; }&#xA;&lt; / script&gt;&#xA;&lt; script async defer&#xA; src =“https://maps.googleapis.com/maps/api/js?key=APIKEY&callback=initMap”&gt ;&#xA;&lt; / script&gt;&#xA;  
&#xA;&#xA;

我的控制器在controller.js

&#xA;& #xA;
  .controller('RouteCtrl',function($ scope,$ ionicLoading){&#xA; $ scope.mapCreated = function(map){&#xA; $ scope.map = map; &#xA;};&#xA;&#xA; $ scope.centerOnMe = function(){&#xA; console.log(“居中”);&#xA; if(!$ scope.map){& #xA; return;&#xA;}&#xA;&#xA; $ scope.loading = $ ionicLoading.show({&#xA; content:'获取当前位置...',&#xA; showBackdrop: false&#xA;});&#xA;&#xA; navigator.geolocation.getCurrentPosition(function(pos){&#xA; console.log('Got pos',pos);&#xA; $ scope.map .setCenter(new google.maps.LatLng(pos.coords.latitude,pos.coords.longitude));&#xA; $ scope.loading.hide();&#xA;},function(error){&# xA;警告('无法获取位置:'+ error.message);&#xA;});&#xA;}&#xA;&#xA;&#xA;&#xA;&#xA;& #xA;})&#XA;  
&#XA; {{66 }} {{11}} 41173547 {{22}} {{33}} JUnit Mockito测试失败,使用equalTo {{44}} {{55}}

我试图理解为什么添加 equalTo 会让我的测试失败。&#xA;我用简单的类和测试重新创建了问题。

&#xA;&#xA;

第一次测试通过,第二次测试失败。

&#xA;&#xA;

示例类

&#xA;&#xA;
 公共类Class {&#xA;&#xA; Map&lt; String ,布尔&gt; data = new HashMap&lt;&gt;();&#xA;&#xA; public Map&lt; String,Boolean&gt; getData(boolean access){&#xA; if(!access)&#xA; return null;&#xA;返回数据;&#xA;}}&#xA;  
&#xA;&#xA;

测试类

&#xA;&#xA;
 < code> public class ClassTest {&#xA;&#xA; @InjectMocks&#xA; private class testObj;&#xA;&#xA; @ Test&#xA; public void testGetDataNull(){&#xA; assertThat(testObj.getData(false),is(nullValue()));&#xA;}&#xA;&#xA; @ Test&#xA; public void testGetDataNull2(){&#xA; assertThat(testObj.getData(false),是(equalTo(nullValue())));&#xA;}}&#xA;  
&#xA;&#xA;

失败的测试显示此错误消息

&#xA;&#xA;
 预期:是&lt; null&gt;&#xA;但是:是null&#xA;  
&#xA;&#xA;

问:这些之间有什么区别?

&#xA;&#xA;

非常感谢任何见解!

&#xA;

1 个答案:

答案 0 :(得分:0)

有两种方法可以解决您的问题:

  1. 将脚本更改为控制器中的angular方法或创建服务,如:

    var marker = new google.maps.Marker({       map:$ scope.map,       动画:google.maps.Animation.DROP,       位置:latLng   });

    var infoWindow = new google.maps.InfoWindow({       内容:&#34;我在这里!&#34;   });

    google.maps.event.addListener(marker,&#39; click&#39;,function(){       infoWindow.open($ scope.map,marker);   });

  2. 在控制器中将其更改为jquery,但不建议这样做,因为它会破坏离子中角度使用的目的:

    var map = new google.maps.Map($(&#34; #map&#34;),{       zoom:4,       中心:乌鲁鲁     });