[英]AngularJS - ng-click and ng-show
I have a map with some markers on it. 我有一张地图上有一些标记。 On click, each marker is opening a overlay-div.
点击后,每个标记都会打开一个overlay-div。
<div class="map" ng-init="loadall()">
<a ng-click="details.show=!details.show" href="#/dealer/{{marker.id}}" class="marker" style="left:{{marker.coordleft}}px;top:{{marker.coordtop}}px" ng-repeat="marker in dealer"></a>
</div>
it's opening this div: 它正在打开这个div:
<div ng-show="details.show">
<div ng-view></div>
</div>
Problem: 问题:
If the div is already open (details.show == true) my customer noticed, that if you leave it open and click on an other marker on the map, the div closes again. 如果div已经打开(details.show == true),我的客户注意到,如果你打开它并点击地图上的另一个标记,则div会再次关闭。
What I want to achieve: 我想要实现的目标:
If the div is already open, just load the new content into the ng-view without closing and reopening the div again. 如果div已经打开,只需将新内容加载到ng-view中,而不关闭并再次重新打开div。
Is this possible? 这可能吗?
EDIT: 编辑:
My routes: 我的路线:
app.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/dealer/:id', {templateUrl: 'files/tpl/dealer-details.html?20', controller: 'DealerDetailsCtrl', activetab: 'details'}).
when('/dealermessage/:id', {templateUrl: 'files/tpl/dealer-message.html?124', controller: 'DealerMessageCtrl', activetab: 'message'}).
when('/dealersendmessage/:id', {templateUrl: 'files/tpl/dealer-details.html?8', controller: 'DealerDetailsCtrl', activetab: 'details'}).
otherwise({redirectTo: '/dealer'});
}]);
If a marker has been clicked, the first route and controller are loading.Is this helping out? 如果单击了一个标记,则第一个路径和控制器正在加载。这有帮助吗?
EDIT No.2: 编辑2:
The "marker/div toggle is working now but I have now a strange behavior of the opened overlay. “marker / div切换现在正在工作,但我现在有一个打开的叠加层的奇怪行为。
Example: 例:
When I open "Marker 1" the div is opening fine. 当我打开“Marker 1”时,div正好打开。 When the div for "Marker 1" is open, I can click on "Marker 2" and the div refreshes with the content of "Marker 2".
当“标记1”的div打开时,我可以单击“标记2”并使用“标记2”的内容刷新div。 But when I now click on "Marker 1" the div suddenly closes.
但是当我现在点击“Marker 1”时,div突然关闭。
How can I stop it? 我怎么能阻止它?
Still don't understand it much, but you keep only one model to toggle several markers . 仍然不太了解它,但你只保留一个模型来切换几个标记 。 So if you click on one marker to open, another marker will close it, since you don't keep the marker's state.
因此,如果您单击一个标记打开,另一个标记将关闭它,因为您没有保持标记的状态。 You probably need something like:
你可能需要这样的东西:
<div ng-repeat="marker in dealer">
<a ng-click="details.show=toggle(marker)" href="#/dealer/{{marker.id}}"
class="marker" style="left:{{marker.coordleft}}px;top:{{marker.coordtop}}px"></a>
</div>
In your controller you define the toggle
function: 在控制器中定义
toggle
功能:
$scope.toggle = function(marker) {
marker.show = !marker.show;
return marker.show;
};
Your ng-click handler needs to have more logic (as @asgoth's answer shows). 您的ng-click处理程序需要具有更多逻辑(如@ asgoth的答案所示)。
Another possible solution (that does not modify the dealer data): pass $event
into your ng-click handler 另一种可能的解决方案(不修改经销商数据):将
$event
传递到ng-click处理程序
<a ng-click="handleOverlay($event)" ... ></a>
Then store the srcElement on $scope ( $scope.openMarker = ev.srcElement
). 然后将srcElement存储在$ scope(
$scope.openMarker = ev.srcElement
)上。 handleOverlay() can then determine, for each click, if the click is happening on the element that is already open, or a new one. 然后,handleOverlay()可以为每次单击确定是否在已打开的元素或新元素上发生了单击。
We don't know how you are reloading ng-view, so if that is an issue, we'd need to see more code. 我们不知道你是如何重新加载ng-view的,所以如果这是一个问题,我们需要看到更多的代码。 Eg, are you loading a different controller for each new view?
例如,您是否为每个新视图加载了不同的控制器? If so, you may need to store the srcElement on a service or rootScope (ie, something that will survive a view/controller change).
如果是这样,您可能需要将srcElement存储在服务或rootScope上(即,在视图/控制器更改后仍然存在的情况)。
So I followed asgoth's solution and also extended it to fix the issue mentioned by Marek123. 所以我遵循了asgoth的解决方案,并将其扩展到修复Marek123提到的问题。 Below is my solution :
以下是我的解决方案:
$scope.selectedMarker = {};
$scope.toggle = function(marker) {
//assuming you have a key "id" in each dealer
if (marker.id !== $scope.selectedMarker.id) {
_.each($scope.delear, function(value, index) {
$scope.delear[index].show = false;
});
}
marker.show = !marker.show;
return marker.show;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.