JavaScript Dynamically disable a span with ng-model

I am following this example for making a custom editable <span> using ngModelController: https://docs.angularjs.org/api/ng/type/ngModel.NgModelController#example

What I want to do now is to be able to dynamically disable editing of this field via logic inside the directive itself. By disabling it I mean the user would not be able to edit the text at all, it would just appear as regular text.

Here is a sample plunkr:

Code from plunkr:

angular.module('app', [])


.controller('Ctrl', function ($scope) {
    $scope.stuff = "test";
  })
  .directive('contenteditable',
    function ($log) {
        'use strict';
        return {
            restrict: 'A',
            require: 'ngModel',
            scope: {
            },
            link: function ($scope, $element, $attributes, ngModel) {
                if (angular.isUndefined(ngModel)) {
                    $log.warn('ngModel is not defined');
                    return;
                }
                function read() {
                    ngModel.$setViewValue($element.text());
                }
                ngModel.$render = function () {
                    $element.html(ngModel.$viewValue || '');
                };
            }
        };
    }
);

Any help would be appreciated.

Answer:1

I have made a dropdown that appears on click on a button. Pretty simple, except for how I have to click twice to get the js function to execute. After I click it the first, it appears and disappears ...

I have made a dropdown that appears on click on a button. Pretty simple, except for how I have to click twice to get the js function to execute. After I click it the first, it appears and disappears ...

  1. having to click twice on everything
  2. having to click mouse twice

My application returns the relative positions between two lines and I want draw this lines with Javascript. The line is defined with a director vector, a point and is three dimension. Is there any ...

My application returns the relative positions between two lines and I want draw this lines with Javascript. The line is defined with a director vector, a point and is three dimension. Is there any ...

  1. drawing with elements of art
  2. drawing elements and principles
  3. drawing with elements and principles of art
  4. drawing that have elements of art
  5. drawing in elements

Firstly, sorry for my poor English. I'm learning AngularJS recently and I found that many of AngularJS's concept are very advanced especially the Directive. It's much like the Shadow DOM or Web ...

Firstly, sorry for my poor English. I'm learning AngularJS recently and I found that many of AngularJS's concept are very advanced especially the Directive. It's much like the Shadow DOM or Web ...

I'm using jQuery UI sortable with data-id attributes. I know you can use sortable('serialize') with something like id="row_4" and this does work for me but I need to do it this way. sortable('...

I'm using jQuery UI sortable with data-id attributes. I know you can use sortable('serialize') with something like id="row_4" and this does work for me but I need to do it this way. sortable('...

  1. jquery sortable serialize example
  2. jquery sortable serialize
  3. jquery sortable serialize empty
  4. jquery sortable serialize ajax
  5. jquery sortable serialize not working
  6. jquery sortable serialize multiple lists
  7. jquery sortable serialize to json
  8. jquery sortable serialize table rows
  9. jquery sortable serialize returns empty string
  10. jquery sortable serialize nested list
  11. jquery sortable serialize php
  12. jquery sortable serialize to array
  13. jquery sortable serialize custom attribute
  14. jqueryui sortable serialize
  15. jquery ui serialize
  16. jquery ui serialize not working
  17. jquery ui sortable serialize data attribute
  18. jquery ui sortable serialize example
  19. jquery ui sortable serialize empty
  20. jquery sortable list serialize