从html访问userid到指令

时间:2017-10-27 05:58:52

标签: angularjs

<div ng-repeat="user in children">

 <a ng-click="swapUser(user.user_id);"></a>

    <div ng-model="player.name" currentuser="user.user_id" inline-edit="parentUser.name" on-save="log('SAVE', value)" on-cancel="log('CANCEL', value)"></div>
</div>

指令:

return {
    scope: {
      model: '=inlineEdit',
      handleSave: '&onSave',
      currentuser: '=',
      handleCancel: '&onCancel'
    },
    link: function(scope, elm, attr) {
    var currentuser = attr.currentuser;
    console.log("Userid", attr.currentuser);
    //Here I want to set the user_id so that i can update respective users
    scope.save = function() {
        var objInput = {};
        objInput[field] = scope.model;

        user.updateConfirmUser(objInput, currentuser)
        .success(function () {
          console.log("Saved");
        });
     }

控制器:

$scope.children = [
       {
            "name" : "Amy",
            "age" : "50",
            "height" : "5",
            "type" : "2",
            "user_id" : "1"
        },{

            "name" : "Rock",
            "age" : "50",
            "height" : "5",
            "type" : "2",
            "user_id" : "2"
        },{
            "name" : "Jimmy",
            "age" : "50",
            "height" : "5",
            "type" : "3",
            "user_id" : "3"
        },{
            "name" : "Michael",
            "age" : "50",
            "height" : "5",
            "type" : "3",
            "user_id" : "4"
        }

];

如何从html中将当前用户访问到我的指令中我将当前用户设置为attr并尝试访问指令,其中显示未定义的user_id。请看一下上面的代码并建议谢谢

1 个答案:

答案 0 :(得分:0)

您正在绑定currentuser,其值为“user.user_id”,因此只会传递user_id。

尝试传递整个对象:currentuser="user"