2015-08-26 71 views
0

我試圖隱藏文本,當點擊模板上的關閉鏈接時,通過在父作用域的上下文中執行它。但它不起作用。有人能告訴我我哪裏出錯了。與自定義指令一起使用時,ng-show不起作用

<!doctype html> 
<html ng-app="parking"> 
<head> 
    <title>Parking</title> 
    <script src="https://code.angularjs.org/1.3.14/angular.min.js"></script> 
    <script> 
     var app=angular.module('parking', []); 
     app.controller('parkingCtrl', function($scope){ 
      $scope.show=true; 
      $scope.alertTopic="Something went Wrong!"; 
      $scope.alertDescription="Try Again!"; 
      $scope.closeAlert=function(){ 
       $scope.show=false; 
      }; 
     }); 
     app.directive('alert', function(){ 
      return { 
       templateUrl:"alert.html", 
       restrict: "E", 
       replace: true, 
       scope: { 
        topic: "=topic", 
        description: "=description", 
        close: "&close" 
       } 
      }; 
     }); 
    </script> 
</head> 
<body ng-controller="parkingCtrl"> 
    <alert 
     ng-show="show" 
     topic="alertTopic" 
     description="alertDescription" 
     close="closeAlert" 
    ></alert> 
</body> 
</html> 

<!--alert.html--> 
<div class="alert"> 
    <span class="alert-topic"> 
    <span ng-bind="topic"></span> 
    </span> 
    <span class="alert-description"> 
    <span ng-bind="description"></span> 
    </span> 
    <a href="" ng-click="close()">Close</a> 
</div>  
+0

從內部html你需要做'$ parent.show',因爲你已經創建了隔離範圍的指令.. **或**你需要在隔離範圍內傳遞該變量 –

回答

1

您需要更改接近= 「closeAlert」 關閉= 「closeAlert()」,如:

<alert 
    ng-show="show" 
    topic="alertTopic" 
    description="alertDescription" 
    close="closeAlert()" 
></alert> 

因爲角度與另一個功能,如果你使用&焦炭example

包裝您的功能
+0

它的工作,謝謝@Tek :) – Shruthi