2017-05-29 74 views
0

我使用angularjs和打字稿在那裏我想創建一個指令如下:angularjs指令參數「FN」不是一個函數

這裏是我的控制器:

export const test1 = { 
 
    template: require('./app.html'), 
 
    controller($scope, $http) { 
 
    
 
      $scope.hello = "hello app"; 
 

 
    } 
 
};

我知道如何使用javascript創建指令,但是如何使用const來創建它,然後導入它。

這是我在JS指令:

export const myDirective = { 
    directive() { 
     return { 
     restrict : "A", 
     template : "<h1>Made by a directive!</h1>" 
    }; 

    } 
}; 

筆者認爲:

<div class="container"> 
<div class="row"> 
    <div ng-repeat="b in data"> 
     <div my-directive></div> 

    </div> 
</div> 
<div> 

我試圖將其導入和使用:

import angular from 'angular'; 
import {ngAnimate} from 'angular-animate'; 
import {ngSanitize} from 'angular-sanitize'; 
import 'angular-ui-bootstrap'; 

import 'angular-ui-router'; 
import routesConfig from './routes'; 

import {hello} from './app/hello'; 
import {test1} from './test1/app'; 
import { myDirective} from './test1/directives' 

import './index.scss'; 

export const app = 'app'; 

angular 
    .module(app, ['ngAnimate', 'ngSanitize','ui.router', 'ui.bootstrap']) 
    .config(routesConfig) 
    .component('app', hello) 
    .component('test1', test1) 
    .directive('myDirective', myDirective) ; 

但是我得到這個錯誤:

Error: [ng:areq] Argument 'fn' is not a function, got Object 
+0

你可以只給控制器的類名稱。像'Controller:test1'一樣。只要確保你有正確的編號和順序的參數。在上面的代碼中,我可以看到該參數不同。你是否意味着導出'類'而不是'const'? –

+0

我不介意使用指令作爲常量(作爲我的示例代碼中的控制器),但其餘代碼如何不同 – dream123

回答

0

您應該註冊directive函數而不是myDirective對象。

.directive('myDirective', myDirective.directive) ; 
相關問題