2015-06-25 197 views
0

我有這樣的指令:角JS指令後鏈接不能訪問生成的元素ID

/*html, enclosed in a ng-repeat directive*/ 
<textarea name="alternativaHtml" id="questao_alternativa_{{$index}}" data-ng-model="alternativa.TextoHtml" data-ck-editor></textarea> 

/*javascript*/ 
angular 
    .module("fluxo_itens.directives") 
    .directive('ckEditor', [function() { 
      return { 
       require: '?ngModel', 
       link: { 
        "post": PostLink 
       } 
      }; 
     }]); 

function PostLink($scope, elm, attr, ngModel) { 
    var ck = CKEDITOR.replace(attr.id); 

    ck.on('pasteState', function() { 
     $scope.$apply(function() { 
      ngModel.$setViewValue(ck.getData()); 
     }); 
    }); 

    ngModel.$render = function (value) { 
     ck.setData(ngModel.$modelValue); 
    }; 
} 

的問題是,當CKEDITOR嘗試創建編輯器實例,它無法找到元素,其中有它的編號屬性dinamycally生成。

+0

我沒有用榆樹[0] .ID在CKEDITOR.replace函數,因爲它會返回我'questao_alternativa _ {{$ index}}',是的,{{$ index}}未編譯 –

回答

0

只是猜測,但你可以試試這個:

<textarea name="alternativaHtml" id="questao_alternativa_{{$index}}" data-ng-model="alternativa.TextoHtml" data-ck-editor editor-id="questao_alternativa_{{$index}}"></textarea> 

指令

angular 
.module("fluxo_itens.directives") 
.directive('ckEditor', [function() { 
     return { 
      scope : { 
      'editorId' : '=' 
      } 
      require: '?ngModel', 
      link: { 
       "post": PostLink 
      } 
     }; 
    }]); 

PostLink

function PostLink($scope, elm, attr, ngModel) { 
var ck = CKEDITOR.replace($scope.editorId); 

ck.on('pasteState', function() { 
    $scope.$apply(function() { 
     ngModel.$setViewValue(ck.getData()); 
    }); 
}); 

ngModel.$render = function (value) { 
    ck.setData(ngModel.$modelValue); 
}; 
} 
+0

嘗試了您的解決方案,但它根本不起作用 –

+0

它拋出:語法錯誤:令牌'{'是表達式[21]中第21列的意外標記[questao_alternativa_ { {$ index}}]從[{{$ index}}]開始。 –

+0

ther is typo in binding''將其更改爲'' –

0

的問題是,CKEDITOR無法找到的元素,因爲該元素的id未在DOM中設置。所以我依靠jQuery來設置de元素的DOM屬性,所以CkEditor可以找到textarea。 我在PostLink功能

var ck = CKEDITOR.replace(attr.id); 

改變了這本:

$(elm).attr("id", attr.id).prop("id", attr.id); 
var ck = CKEDITOR.replace(elm[0].id); 

現在一切正常