2017-05-26 76 views
0

我的HTML是如何將角色的參數傳遞給指令函數?

<ul id="suggestions" class="suggestions-list"><li 
     ng-repeat="suggestion in suggestions track by $index" 
     class="suggestion-item" 
     ng-click="toggleSkill(suggestion, 'here')" 
     ng-class="{active : selectedIndex === $index}" 
    ><span class="small clr-secondary">{{suggestion}} - {{$index}}</span></li></ul> 

而在該指令,我有:

link: function(scope, elem, attrs) { 
    scope.toggleSkill = function(item, index) { 
    debugger 
    SkillsService.searchResults = [] 

    if (scope.selectedTags.indexOf(scope.suggestions[index]) === -1) { 
     scope.selectedTags.push(scope.suggestions[index]) 
     scope.searchText = "" 

出於某種原因,index在功能就會出現,undefined。爲什麼會這樣?

+0

真的很奇怪......建議是否正確傳遞?如果你在你的函數內部嘗試'console.log(arguments);',你能在某處看到'here'參數嗎? – quirimmo

+0

'建議'正確通過 – Shamoon

回答

1

你的代碼工作正常。這裏有一個更簡潔的版本,便於閱讀。檢查這是否工作。

<!--inside ur template--> 
<p class="text" ng-click="zoo('param1', 'param2')">click me</p> 

// inside ur directive 
$scope.zoo = function (x, y) { 
    console.log('my params ', x, y); 
} 
-1

試:當我複製粘貼它的模板/指令內

ng-click="toggleSkill(suggestion, $index)" 
+0

因爲您已經在參數中有建議實例,所以不需要使用scope.suggestions [index] 。 如果該項目不存在於scope.selectedTags然後 scope.selectedTags.push(項目) 你明白我嗎? (對不起,我的英語) –