用ng-model动态禁用一个span

Dynamically disable a span with ng-model

本文关键字:一个 span ng-model 动态      更新时间:2023-09-26

我正在使用ngModelController制作自定义可编辑的<span>的示例:https://docs.angularjs.org/api/ng/type/ngModel.NgModelController示例

我现在想做的是能够通过指令本身内的逻辑动态禁用此字段的编辑。通过禁用它,我的意思是用户将无法编辑文本,它只是显示为普通文本。

这是一个样品:

代码来自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 || '');
                };
            }
        };
    }
);

感谢gitter中非常有帮助的人。在AngularJS的空间里,我意识到contentEditable是一个实际的html5 api,这就是使span元素可编辑的原因,而不是AngularJS。

我所要做的就是改变指令的名称并动态地删除contentEditable