2017-03-16 32 views
2

我正在研究angular2應用。我有一個要求自動化textarea。 我想重用https://github.com/stevepapa/angular2-autosize在angular2中autosize textarea

其次自述的angular2,自動調整大小,但是我提示以下錯誤:

未捕獲的錯誤:模塊構建失敗:錯誤:ENOENT:沒有這樣的文件或目錄,打開「 C:\用戶\ VIPIN \ SampleApp \ node_modules \ angular2-自動調整大小\ angular2-autosize.js'。

請建議如何解決此問題。

回答

5

我今天有同樣的問題,並得到修復! 請檢查我的叉: https://github.com/chrum/angular2-autosize

直到PR合併嘗試:

npm install https://github.com/chrum/angular2-autosize.git --save 

然後在你的代碼,因爲它是略有不同的,你只需要導入模塊不是指令......

所以 ,而不是

import {Autosize} from 'angular2-autosize'; 

@NgModule({ 
    ... 
    declarations: [ 
    Autosize 
    ] 
    ... 
}) 

你應該有

import {AutosizeModule} from 'angular2-autosize'; 

@NgModule({ 
    ... 
    imports: [ 
    AutosizeModule 
    ] 
    ... 
}) 
4

你可以像這樣不使用包。 其簡單的

控制器像下面

autogrow(){ 
    let textArea = document.getElementById("textarea")  
    textArea.style.overflow = 'hidden'; 
    textArea.style.height = '0px'; 
    textArea.style.height = textArea.scrollHeight + 'px'; 
} 

和HTML像下面

<textarea id="textarea" (keyup)="autogrow()" ></textarea> 
+4

雖然這個工作,它打破了角的理念(即不直接操作DOM)。而不是使用document.getElementById,使用@ViewChild獲取textarea的引用(因爲它畢竟是組件的子元素) – rmcsharry