如何在angucomplete-alt selected-object属性上使用回调函数?

时间:2015-04-15 15:58:47

标签: angularjs

我目前正在处理以下代码:

<angucomplete-alt id="skill1"
                selected-object="addSkill1()"
                remote-url="@Url.Action("GetSkills", "ManageMission")/?query="
                title-field="Name"
                minlength="1"
                match-class="angucomplete-match"
                input-class="form-control"
                template-url="@Url.Content("~/Templates/angucomplete-alt.html")"></angucomplete-alt>
$scope.addSkill1 = function () {
    console.log(arguments); 
};

通过使用回调函数,我试图获取所选对象值(as explained in angucomplete-alt documentation),但我收到以下错误:

  

错误:[$ compile:nonassign] Expression&#39; addSkill1()&#39;用于   指令&#39; angucompleteAlt&#39;是不可转让的!

2 个答案:

答案 0 :(得分:11)

我从Angucomplete Alt挖掘了示例,我发现你必须像这样提供selected-object,即使它是回调函数:

<angucomplete-alt id="skill1"
                selected-object="addSkill"
                remote-url="@Url.Action("GetSkills", "ManageMission")/?query="
                title-field="Name"
                minlength="1"
                match-class="angucomplete-match"
                input-class="form-control"
                template-url="@Url.Content("~/Templates/angucomplete-alt.html")"></angucomplete-alt>

回调方法应该接收selected项作为参数:

$scope.addSkill = function (selected) {
    console.log(selected); 
};

答案 1 :(得分:0)

为了在ng-repeat的范围内传递,我在控制器中创建了一个初始化函数,它将回调函数返回到该作用域。

<html>
    <div ng-repeat="row in rows">
             ...
        <div ng-init="row.callbackFunction = initialiseCallback(row)"></div>
        <angucomplete-alt selected-object = "row.callbackFunction" />
             ...
    </div>
</html>
______________________
Controller() {
    $scope.initialiseCallback = function (row){
        return function (selected) {
            console.log(selected);  
            console.log(row); // should print the row number
        }
    }
}