2015-08-31 67 views
0

我在填充模态窗口形式时遇到了问题。例如,我点击网格行来编辑用户,然后我调用ajax来返回用户的特定数据。填充模态窗口形式

当前代码:

<modal title="Uredi uporabnika" visible="showModal"> 
     <form role="form"> 
      <div class="form-group"> 
      <label for="user_name">Ime</label> 
      <input type="text" class="form-control" id="user_name" /> 
      </div> 
      <button type="submit" class="btn btn-default">Submit</button> 
     </form> 
</modal> 

控制器:

$scope.openUserEditor = function(selected_user_id){ 
    $http({ 
     method:'POST', 
     url:ajax, 
     dataType: 'json', 
     headers: { 
      'Content-type': 'application/x-www-form-urlencoded;charset=utf-8' 
     }, 
     data:{ 
      action:'loadUserData', 
      id:selected_user_id 
     } 
    }).success(function(data,status){ 
      $scope.userInfo = data.user_info; 
      $scope.showModal = !$scope.showModal; 
    }); 
} 

模态窗口代码:

app.directive('modal', function(){ 
    return { 
    template: '<div class="modal fade">' + 
     '<div class="modal-dialog">' + 
     '<div class="modal-content">' + 
      '<div class="modal-header">' + 
      '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>' + 
      '<h4 class="modal-title">{{ title}}</h4>' + 
      '</div>' + 
      '<div class="modal-body" ng-transclude></div>' + 
     '</div>' + 
     '</div>' + 
    '</div>', 
    restrict: 'E', 
    transclude: true, 
    replace:true, 
    scope:true, 
    link: function postLink(scope, element, attrs) { 
    scope.title = attrs.title; 

    scope.$watch(attrs.visible, function(value){ 
     if(value == true) 
     $(element).modal('show'); 
     else 
     $(element).modal('hide'); 
    }); 

    $(element).on('shown.bs.modal', function(){ 
     scope.$apply(function(){ 
     scope.$parent[attrs.visible] = true; 
     }); 
    }); 

    $(element).on('hidden.bs.modal', function(){ 
     scope.$apply(function(){ 
     scope.$parent[attrs.visible] = false; 
     }); 
    }); 
    } 
}; 
}); 

我会apreciate任何帮助,即使我在becouse正确的方式在做我是Angular的新手。

+0

你在哪里和什么时候调用openUserEditor函数? – Shivi

+0

在表行点击'​​ \t \t \t <按钮类= “BTN” NG单击= “openUserEditor(user.id)”> \t \t \t编辑 \t \t \t \t \t \t' – Ales

+0

可以你请检查你的成功回调中的$ scope对象.. ??有时它被设置为窗口,而不是控制器范围。然后,您可以在api调用之外创建一个变量,如var self = this,然后使用this.userInfo。 – Shivi

回答

0

使用输入ID来填充值:

$("#user_name").val(data.user_info); 
0

好吧,我发现解决数以百万计的不同改掉后,我一直只是把双手在我的头上。因此我不得不添加userInfo对象并调用该参数u_name。

tnx Shivi无论如何都帮忙:D