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">×</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的新手。
你在哪里和什么时候调用openUserEditor函数? – Shivi
在表行点击' \t \t \t <按钮类= “BTN” NG单击= “openUserEditor(user.id)”> \t \t \t编辑 \t \t \t \t \t \t' – Ales
可以你请检查你的成功回调中的$ scope对象.. ??有时它被设置为窗口,而不是控制器范围。然后,您可以在api调用之外创建一个变量,如var self = this,然后使用this.userInfo。 – Shivi