单击时更改父元素的 AngularUI 弹出窗口样式

AngularUI popover style change of parent element on click

我有一个非常具体的问题。最近我想出了如何 . Originally the user had to click on the popover parent to open and to close it, so I had functionality added in that changed the style of that element on a click event. With the addition of the close button, I want to simulate the same effect when the user clicks it. I set up a plnkr here 来玩。

到目前为止,我已经尝试在弹出框模板中添加 ng-click="toggle(); style=!style",但没有成功。我认为我必须访问 popoverToggle 指令中的 style 变量,但我不知道该怎么做。也可以在 HTML.

中对其进行操作

弹出框模板

<div><span ng-click="toggle()">X</span></div>
<p>content</p>

popoverToggle.js

angular.module('app')
    .config(function($tooltipProvider) {
        $tooltipProvider.setTriggers({'open': 'close'});
      })

      .directive('popoverToggle', function($timeout) {
        return {
          scope: true,
          link: function(scope, element, attrs) {
            scope.toggle = function() {
              $timeout(function() {
                element.triggerHandler(scope.openned ? 'close' : 'open');
                scope.openned = !scope.openned;
                scope.style = !scope.style; // doesn't do anything either
              });
            };
            return element.on('click', scope.toggle);
          }
        };
      });

index.html

<span ng-class="{'border-gray': style}" style="margin: 40px" ng-click="style=!style" class="red-btn">
  <span popover-placement="bottom" popover-template="'popover-template.html'"
  popover-trigger="open" popover-append-to-body="true" popover-toggle>click me</span>
</span>

解决方案非常straightforward,只需要更改style变量即可。只需将其更改为 $scope.style,如下所示:

popover-template.html

<div><span ng-click="toggle(); $scope.style=!$scope.style">X</span></div>
<p>content</p>

index.html

<span ng-class="{'border-gray': $scope.style}" style="margin: 40px" ng-click="$scope.style=!$scope.style" class="red-btn">
  <span popover-placement="bottom" popover-template="'popover-template.html'"
  popover-trigger="open" popover-append-to-body="true" popover-toggle>click me</span>
</span>