2017-05-26 77 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(项目) 你明白我吗? (对不起,我的英语) –