ui-ace icon indicating copy to clipboard operation
ui-ace copied to clipboard

ng-model of ace editor is not changing when I'm dynamically changing few lines of editor.

Open prashant-pokhriyal opened this issue 8 years ago • 6 comments

Firstly I'm loading content of editor by initialising ng-model of editor. Then in controller I'm changing few lines, but when I'm printing ng-model value it is showing the previous one.

prashant-pokhriyal avatar Aug 30 '17 11:08 prashant-pokhriyal

Hey @prashant-pokhriyal did you find a solution to this issue?

mirwaqaskhan avatar Oct 25 '17 08:10 mirwaqaskhan

If you are looking for 2 way binding then this is how it works.

In Html:

<div ui-ace="{
                        useWrapMode : true,
                        showGutter: true,
                        mode: 'json',
                        firstLineNumber: 5,
                        onLoad : aceLoaded,
                        onChange: aceChanged
                      }" ng-model="json.jsonStrifyJsonData"></div>

in controller:

$scope.json = {
      jsonStrifyJsonData: JSON.stringify(YOUR_JSON_OBJECT, null, 4)
    }

You need to set the model as a JSON object so that the scopes issues do not arise.

mirwaqaskhan avatar Oct 25 '17 12:10 mirwaqaskhan

So with this approach, ace should update the model value whenever the value of editor changes. right?

In my case, instead of a human interaction with the editor, I have a button which when clicked inserts a text in the editor but same is not reflected in the model.

Here's the Fiddle for the same - https://jsfiddle.net/ashitvora/q27ebeeq/38/

when I insert the text, I do it in Editor but not in Model Value. When I explicitly set the value to model, the cursor moves the very first position instead of staying there.

ashitvora-zz avatar Feb 26 '18 08:02 ashitvora-zz

If your goal is to update Editor with some buttons you can easily do that with updating the model on the click handler of your button and it would work. Updated fiddle: https://jsfiddle.net/q27ebeeq/41/

and for the cursor moment: that happens because the cursor position is zero when there is no user interaction with the editor. You would need to calculate the desired cursor position and set the cursor position to that point.

mirwaqaskhan avatar Feb 26 '18 09:02 mirwaqaskhan

I did try calculating the cursor position manually and setting it but it doesn't work. Here's the code for the same...

var param = Math.random();

// Get current cursor position to insert text
var cursorPosition = scope.aceUrlEditor.getCursorPosition();

// Inser text
scope.aceUrlEditor.session.insert(cursorPosition, param);

// Assign Ace Content to Model as it doesn't do it automatically.
scope.operator.dynurl.value = scope.aceUrlEditor.getValue();

// Set cursor right after the newly added text
scope.aceUrlEditor.renderer.scrollCursorIntoView({
	row: cursorPosition.row,
	column: cursorPosition.column + param.length
}, 0.5);

ashitvora-zz avatar Aug 21 '18 06:08 ashitvora-zz

If your goal is to update Editor with some buttons you can easily do that with updating the model on the click handler of your button and it would work.

In that case you'll loose the undo/redo history

smellai avatar Dec 13 '18 10:12 smellai