1

是否无法使用双向绑定通过隔离范围将值从父范围设置为 contentEditable 指令?

代码在这里:http: //jsfiddle.net/bharatwaj/3wTd3/5

HTML:

    <input ng-model="foo" />
    <div contentEditable="true" binding-foo="foo" ng-model="input.name" title="Click to     edit"></div> 
    <pre>model = {{input.name}}</pre>

JS:

    angular.module('form-example2', []).directive('contenteditable', function () {
        return {
        scope: {
            isolatedBindingFoo: '=bindingFoo'
        },
        require: 'ngModel',
        link: function (scope, elm, attrs, ctrl) {
            console.log('isolatedBindingFoo value is ' + scope.isolatedBindingFoo);
            // view -> model
            elm.bind('blur', function () {
            scope.$apply(function () {
                ctrl.$setViewValue(scope.isolatedBindingfoo);
            });
            });

            // model -> view
            ctrl.$render = function () {
            elm.html(ctrl.$viewValue);
            };

            // load init value from DOM
            // Why is content editable value displayed as Hello!
            // after setting view value below?
            ctrl.$setViewValue(scope.isolatedBindingFoo);
        }
        };
    });

    function ItemCtl($scope) {
         $scope.foo = 'Hello!';
    }

我有一个设置为“你好!”的 ng-model 在控制器中并且是两个绑定到内容可编辑 div

不应该 contentEditable div 显示你好!?

注意:可以像下面这样通过插值进行设置,但我想知道是否不能通过范围内的两个绑定进行设置。

{{foo}}
4

0 回答 0