我使用Code Mirror指令將文本區域設置爲代碼格式。如何使用變量將對象鍵傳遞給Angular指令?
什麼是工作:
<textarea ui-codemirror type="textarea" ng-model="x"></textarea>
您可以設置選項像這樣
<textarea ui-codemirror="editorOptions" type="textarea" ng-model="x"></textarea>
,並在你的控制器:
$scope.editorOptions = {
name: 'javascript',
json: true,
smartIndent: false,
tabSize: 2,
lineWrapping : true,
lineNumbers: true,
mode: 'javascript'
}
什麼不工作:
我想動態更改基於模型的另一部分(我支持JavaScript和Markdown)的editorOptions。在HTML
$scope.editorOptions = {
json: {
name: 'javascript',
json: true,
smartIndent: false,
tabSize: 2,
lineWrapping : true,
lineNumbers: true,
mode: 'javascript'
},
markdown: {
name: 'markdown',
json: false,
smartIndent: false,
tabSize: 2,
lineWrapping : true,
lineNumbers: true,
mode: 'markdown'
}
};
然後將此:
所以我想這個
<select ng-model='editorType' required ng-options='option.value as option.name for option in editorTypes'></select>
<textarea ui-codemirror="editorOptions[editorType]" type="textarea" ng-model="x"></textarea>
我的問題是 - 如何使用選擇模型(editorType),它的值指定在代碼鏡像指令中使用哪些選項對象?
我已經試過
<textarea ui-codemirror="editorOptions.[editorType]" type="textarea" ng-model="x"></textarea>
<textarea ui-codemirror="editorOptions[$scope.editorType]" type="textarea" ng-model="x"></textarea>
都無濟於事。
任何人都知道正確的方法是什麼?
很多,非常感謝!
更新 我相信這是正確的語法:
ui-codemirror="editorOptions[editorType]".
我認爲這是與指令不實現該變量的一個問題發生了變化。
可以ü分享您的提琴或plunker演示 –
我相信問題可能是指令是不知道的是,$ scope.editorType變量發生了變化。 –
@Hairgami_Master我認爲你很可能咆哮正確的嘗試,祝你好運。 – shaunhusain