我想提出一个应用程序,在那里我有很多输入字段。 这些输入字段从JSON对象数组字段产生与AngularJS ngRepeat指令和旁边有一个按钮,用于打开角UI引导模式在一个更大的文本区域来编辑该值。 我无法弄清楚如何这个模型属性参考角UI引导,这样我可以节省模式所做的更改。 由于需要在多个视图中此功能,我把它变成一种服务。
I have made a plunker to illustrate my problem.
http://plnkr.co/edit/ZrydC5UExqEPvVg7PXSq?p=preview
目前在plunker例如模式包含textarea的,但我确实需要使用文本角指令,因为这些字段包含一些HTML标记,我会更容易为用户与这个漂亮的插件编辑值。
TextAngular
编辑:请,如果你正在服用的时间来回答,你可能会藏汉需要多一点的时间来修改我的plunker例如精确显示您的解决方案会是什么样子,因为似乎每个人谁试图帮助我,认为他们知道解决的办法,但在现实中它不工作:(谢谢!
我个人比较喜欢装饰我的$范围的服务(即$ scope.modalService = ModalService),所以我理解其中的逻辑之源。 在NG-重复你那么该值项传递给方法调用:
<div class="input-group">
<input class="form-control" ng-model="value.value">
<span class="input-group-addon" ng-click="modalService.openTextEditModal(value)">
<span class="glyphicon glyphicon-align-justify"></span>
</span>
</div>
然后,模式和服务模式的模板将引用的对象,在这种情况下,物体的克隆,以帮助国家管理,而不是文本:
app.factory('ModalService', function($modal) {
return {
openTextEditModal: function(item) {
var modalInstance = $modal.open({
templateUrl: 'modal.html',
backdrop: 'static',
controller: function($scope, $modalInstance, $sce, item) {
var clone = {};
angular.copy(item, clone);
$scope.clone = clone;
$scope.close = function() {
$modalInstance.dismiss('cancel');
};
$scope.save = function() {
angular.extend(item, clone);
$modalInstance.close();
};
},
size: 'lg',
resolve: {
item: function() {
return item;
}
}
});
}
};
});
与相应的模态模板更改:
<div class="modal-header">
<h3 class="modal-title">Edit text</h3>
</div>
<div class="modal-body">
<textarea class="form-control" ng-model="clone.value"></textarea>
</div>
<div class="modal-body">
<button class="btn btn-warning" ng-click="close()">Close</button>
<button class="btn btn-success pull-right" ng-click="save()">Save</button>
</div>
这可能是容易使控制器为您的模式,并通过在您从范围需要的对象。 这些将通过引用传递所以改变它们会更新你的父母范围的范围。 像这样的事情在你的MainCtrl:
var modalInstance = ModalService.open({
templateUrl: 'modal.html',
controller: 'YourModalController',
size: 'lg',
resolve: {
text: function () {
return $scope.editText;
}
}
});
modalInstance.result.then(function () {
});
然后在你的模态控制器:
app.controller('YourModalController', ['$scope', '$modalInstance', 'text', function YourModalController($scope, $modalInstance, text) {
$scope.text = text;
$scope.close = function() {
$modalInstance.dismiss('cancel');
};
$scope.save = function() {
$modalInstance.close($scope.text);
};
}]);
如果你希望它是可重复使用的,所以你不必重复父控制器的模式实例代码,你可以做一个指令。
您可以返回的承诺,然后在处理控制器中的成功回调。
在openTextEditModal
函数, return modalInstance.result;
。
然后,在控制器,你可以这样做:
$scope.editText = function(text){
ModalService.openTextEditModal(text).then(function(newText){
console.log(newText);
$scope.text = newText; // do something with the new text
});
};