2014-06-16 20 views
0

我得到這個錯誤: TypeError:無法讀取屬性'preventDefault'未定義在Scope。$ scope.dispatchAction。

我找不出什麼問題,請你幫我一下?謝謝。

---- ----模板

$templateCache.put("template/dropdown-user.html", 
    '<ul class="dropdown-menu dropdown-user">'+ 
     '<li ng-repeat-start="item in items">'+ 
      "<a ng-click=\"click(item,$event)\" ng-href=\"{{item.link}}\"><i class=\"fa fa-{{item.icon}} fa-fw\"></i> {{item.label}}</a>"+ 
     '</li>'+ 
     "<li ng-repeat-end class=\"divider\" ng-if=\"item.isDivider\"></li>"+ 
    '</ul>' 
    ); 

---指令---

.directive('navbarMenuUserItem',[function(){ 
    return { 
     restrict: 'E', 
     templateUrl: 'template/dropdown-user.html', 
     replace: true, 
     scope:{ 
      items: '=', 
      click: '&itemClick' 
     } 
    } 
}]) 

--- HTML-- -

<navbar-menu-user-item item-click="dispatchAction(obj,event)" items="usermenu"></navbar-menu-user-item> 

控制器*

app.controller('dashboardCtrl', 
['$scope','$sanitize','$log','$location','dashboardService','AuthService', 
    function($scope, $sanitize, $log, $location, dashboard,AuthService){ 

     $scope.messages = dashboard.getMessages(); 
     $scope.usermenu = dashboard.getUserMenu(); 
     $scope.sidebarmenu = dashboard.getSidebar(); 

     $scope.logout = function(){ 
      AuthService.logout(); 
      $location.path('/'); 
     }; 

     $scope.dispatchAction = function(obj,event){ 
      event.preventDefault(); 
      console.log(obj); 
     }; 


    } 
]); 
+0

@KhanhTO,我剛加了$,但還是沒有運氣,謝謝。 –

+2

您使用錯誤的語法來調用範圍函數綁定。在模板dropdown-user.html –

+0

中點擊「 maurycy

回答

1

汗傳輸機會的建議是正確的。你必須將你的項目和你的事件綁定到你的表達item-click應該被評估的範圍。

Alternativly你可以改變你的孤立範圍定義的指令:

scope:{ 
     items: '=', 
     click: '=itemClick' 
    } 

和你item-click在HTML中的屬性:

item-click="dispatchAction" 

這樣,你直接綁定您的dispatchAction功能指令範圍中的一個變量。