Tyler Murry
Tyler Murry

Reputation: 2805

Copy by value in Angular not working properly

I am trying to build a simple editable div page where you can edit, cancel and save the contents. Clicking edit seems to enable the div for editing properly. However when I save, the value is not synchronized with what was entered. Additionally, when you cancel and need to revert back to what was there before, it does not pull the right value.

I have attempted to store the previous contents temporarily by concatenating the current value with and empty string to prevent copying a reference, but I'm failing to see where I'm messing up at.

I have created a simple example to demonstrate: http://play.ionic.io/app/76fa89478b4c

Upvotes: 0

Views: 120

Answers (1)

jeremy-denis
jeremy-denis

Reputation: 6878

It's because content editable is not working totally with angular.

replace your div with contenteditable by :

<input ng-model="contents" ng-disabled="!editingContents"/>

your html become like that :

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <link href="https://code.ionicframework.com/1.0.0/css/ionic.min.css" rel="stylesheet">
    <script src="https://code.ionicframework.com/1.0.0/js/ionic.bundle.js"></script>
  </head>
  <body ng-app="app" ng-controller="TestController">
    <ion-pane>
      <ion-header-bar class="bar-stable">
        <h1 class="title">Awesome App</h1>
      </ion-header-bar>
      <ion-content class="padding">
        <input ng-model="contents" ng-disabled="!editingContents"/>
        <br/>
        <button class="button button-dark" ng-click="edit()">edit</button>
        <button class="button button-dark" ng-click="cancel()">cancel</button>
        <button class="button button-dark" ng-click="save()">save</button>
      </ion-content>
    </ion-pane>
  </body>
</html>

Upvotes: 1

Related Questions