2
A
回答
1
更新:修訂版可用角1.3在http://codepen.io/maxbates/pen/AfEHz
我做了一個指令,做到這一點。 如果您有任何建議
據我知道,我將編輯它,有沒有辦法取消在$解析器管道模型更新(和看ngModelCtrl源似乎驗證這一點)除了添加一個函數哪些錯誤。
請注意,您不直接在textarea上使用ng模型。此解決方案添加了自己的ngModel,綁定到中間對象,並且只會在JSON有效時傳播更改。
'use strict';
/*
example usage: <textarea json-edit="myObject" rows="8" class="form-control"></textarea>
jsonEditing is a string which we edit in a textarea. we try parsing to JSON with each change. when it is valid, propagate model changes via ngModelCtrl
use isolate scope to prevent model propagation when invalid - will update manually. cannot replace with template, or will override ngModelCtrl, and not hide behind facade
will override element type to textarea and add own attribute ngModel tied to jsonEditing
As far as I know, there is currently no way to achieve this using $parsers (other than one of the function errors and kills the pipeline)
*/
angular.module('myApp')
.directive('jsonEdit', function() {
return {
restrict: 'A',
require: 'ngModel',
template: '<textarea ng-model="jsonEditing"></textarea>',
replace : true,
scope: {
model: '=jsonEdit'
},
link: function (scope, element, attrs, ngModelCtrl) {
function setEditing (value) {
scope.jsonEditing = angular.copy(JSON2String(value));
}
function updateModel (value) {
scope.model = string2JSON(value);
}
function setValid() {
ngModelCtrl.$setValidity('json', true);
}
function setInvalid() {
ngModelCtrl.$setValidity('json', false);
}
function string2JSON(text) {
try {
return angular.fromJson(text);
} catch (err) {
setInvalid();
return text;
}
}
function JSON2String(object) {
// better than JSON.stringify(), because it formats + filters $$hashKey etc.
// NOTE that this will remove all $-prefixed values
return angular.toJson(object, true);
}
function isValidJson(model) {
var flag = true;
try {
angular.fromJson(model);
} catch (err) {
flag = false;
}
return flag;
}
//init
setEditing(scope.model);
//check for changes going out
scope.$watch('jsonEditing', function (newval, oldval) {
if (newval != oldval) {
if (isValidJson(newval)) {
setValid();
updateModel(newval);
} else {
setInvalid();
}
}
}, true);
//check for changes coming in
scope.$watch('model', function (newval, oldval) {
if (newval != oldval) {
setEditing(newval);
}
}, true);
}
};
});
相關問題
- 1. 如何將JSON字符串綁定到GWT標籤中?
- 2. 綁定JSON字符串DROPDOWNLIST
- 3. 如何將JSON字符串綁定到真實的對象定義?
- 4. 如何將字符串轉換爲角度綁定?
- 5. 將json字符串綁定到asp.net中的模型MVC
- 6. 將TextArea換行符綁定到html
- 7. 如何將JSON字符串化的JavaScript對象發送到
- 8. 在Metro應用中將JSON字符串綁定到ListView?
- 9. 將JSON字符串數據綁定到詳細信息視圖
- 10. 將RichTextBox綁定到字符串
- 11. 將RichTextBox.Text數據綁定到字符串
- 12. 將字符串數組綁定到DropDownList?
- 13. 將字符串集合綁定到FlowDocument
- 14. 將字符串綁定到TableView/TextView ID
- 15. 將複選框綁定到字符串
- 16. 如何將導入的ObservableCollection的字符串綁定到ItemsControl?
- 17. 將綁定網格字段綁定到XML字符串元素
- 18. JSON使用json字符串將對象字符串化爲值
- 19. 如何將簡單的字符串值綁定到文本框?
- 20. 如何將字符串添加到圖像源的綁定值?
- 21. 如何將ComboBox的SelectedValue屬性綁定到字符串屬性?
- 22. 如何將字符串綁定到Guice中的變量?
- 23. 如何將連接字符串綁定到我的SqlDataSource
- 24. 角度textarea數據綁定
- 25. 串行化字符串數組到json
- 26. 如何將DateTime綁定到MaskedTextBox時應用格式化字符串?
- 27. Highcharts將字符串化JSON的選項
- 28. 如何將JSON值轉換爲角度4中的字符串?
- 29. 如何綁定observableArray中的json字符串?
- 30. 如何將devexpress報告表綁定到字符串列表?