2015-09-14 41 views
0

我建立了一個指令,但不在頁面上顯示?這是代碼:如何顯示角度指令?

app.directive('resetFilter', function() { 
    return { 
    restrict:'E', 
    template:'<div>  This is the filter:   <a href="#" ng-click="resetFilter()">Reset Filter</a>    </div>', 
    controller:function($scope){ 
     $scope.resetFilter= function(){ 
     console.log('resetfilter this one !!!!'); 
     $scope.name='' 
     console.log('accname',$scope.name); 
     }; 
    } 
    }; 
}); 

HTML如下:

<body ng-controller="MainCtrl"> 
<reset-filter></reset-filter> 
     name: {{hello}} 
     <input type="text" ng-model='hello' /> 
</body> 

此外,我怎麼能觸發resetFilter功能?我想重置$ scope.name的值? plunk: http://plnkr.co/edit/rUCMq9?p=preview

回答

1

檢查了這一點。

http://plnkr.co/edit/9DjCYfXqvkD7kURpTHCv?p=preview

app.controller('MainCtrl', function($scope) { 

    $scope.hello='hell2o'; 

}); 

app.directive('resetFilter', function() { 
    .... 
}); 

您必須添加控制器外的指令。

至於「重置價值」,檢查這一個AngularJS - pass function to directive

基本上,你需要在你的控制器創建「復位」功能,然後通過它在你的指令,你把它放在ng-click

讓我知道這是否有幫助。

+0

@Pindakaas如果你仍然無法弄清楚。你可以查看這個http://plnkr.co/edit/NjpjO18qH53QQv0LW811?p=preview。 – kdlcruz

+0

很酷的傢伙 – Pindakaas