2017-10-20 16 views
0

코드에서 각도 데이터 테이블을 사용하는 중에 TypeError가 발생합니다. 이 지침에서DataTable이 함수가 아닙니다. 각도 4에 각도 데이터 테이블을 사용하는 중에 TypeError가 발생합니다.

ERROR TypeError: $(...).DataTable is not a function 
at angular-datatables.directive.js:38 

코드는 다음과 같습니다

DataTableDirective.prototype.displayTable = function() { 
    var _this = this; 
    this.dtInstance = new Promise(function (resolve, reject) { 
     Promise.resolve(_this.dtOptions).then(function (dtOptions) { 
      // Using setTimeout as a "hack" to be "part" of NgZone 
      setTimeout(function() { 
       //Error in this line 
       var dt = $(_this.el.nativeElement).DataTable(dtOptions); 
       resolve(dt); 
      }); 
     }); 
    }); 
}; 

문제가 여기에 무엇입니까? 라이브러리 및 종속성을 다음에 의해 설치했습니다 :

npm install jquery --save 
npm install datatables.net --save 
npm install datatables.net-dt --save 
npm install angular-datatables --save 
npm install @types/jquery --save-dev 
npm install @types/datatables.net --save-dev 

어떤 생각입니까?

업데이트 :

{ 
    "extends": "../tsconfig.json", 
    "compilerOptions": { 
    "outDir": "../out-tsc/app", 
    "baseUrl": "./", 
    "module": "es2015", 
    "allowSyntheticDefaultImports": true 
}, 
    "exclude": [ 
    "test.ts", 
    "**/*.spec.ts" 
    ] 

}

및 tsconfig.json : 여기 내 tsconfig.app.json입니다

{ 
    "compileOnSave": false, 
    "compilerOptions": { 
    "outDir": "./dist/out-tsc", 
    "sourceMap": true, 
    "declaration": false, 
    "moduleResolution": "node", 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true, 
    "target": "es5", 
    "typeRoots": [ 
     "node_modules/@types" 
    ], 
    "lib": [ 
     "es2017", 
     "dom" 
    ], 
    "allowSyntheticDefaultImports": true,  
    "paths": { 
     "@angular/*": [ 
     "../node_modules/@angular/*" 
     ] 
    } 
    } 
} 
+0

빈 유형 array ("유형": [])가있는 경우 tsconfig.json 및 해당 하위 파일 (예 : tsconfig.app.json)을 확인하십시오. – omeralper

+0

나는 그것을 가지고 있었지만 나는 그것을 제거했지만 변화는 없었다. – Burak

+0

angular-cli.json 파일에 모든 파일을 추가 했습니까? – omeralper

답변

0

안녕하세요 확실하다 _this가 올바르게 함께 잘 살고 당신의 이것의 구성 요소? .. 같은 것을 시도해 보셨습니까?

DataTableDirective.prototype.displayTable =() => { 
    var _this = this; 
    this.dtInstance = new Promise(function (resolve, reject) { 
     Promise.resolve(_this.dtOptions).then(function (dtOptions) { 
      // Using setTimeout as a "hack" to be "part" of NgZone 
      setTimeout(function() { 
       //Error in this line 
       var dt = $(_this.el.nativeElement).DataTable(dtOptions); 
       resolve(dt); 
      }); 
     }); 
    }); 
}; 

희망이 있습니다!

+0

Jquery 및 jquery 확장은 전역 적입니다. 왜 그것이 폐쇄 문제에 관한 것이라고 생각합니까? – omeralper

+0

왜냐하면 매번 내 TS 코드에 함수를 넣을 때마다이 범위가 느슨해졌습니다. 어쩌면 뚱뚱한 화살표 함수를 사용해보십시오. 도움이되기를 바랍니다! –

+0

아 .. angular-cli.json 파일에 datatable.js 플러그인을 포함시켜야합니다. 프로토 타입 –