2015-04-06 119 views
1

我正在測試angular和browserify。假設簡單而直截了當地下降。只有它不是:(這裏是我的問題:Angular,browserify,指令控制器不可用

我有一個目錄結構在我src文件夾中,像這樣:

src/ 
js/ 
    controller/ 
    index.js 
    controller1.js 
    ...... 
    directive/ 
    index.js 
    directive1.js 
    directive2.js 

我的指令/ index.js看起來是這樣的:

'use strict'; 

var app = angular.module('seisTools'); 
app.directive('fillController', require('./fill_controller')); 
app.directive('fill', require('./fill')); 

我的問題指令看起來像這樣(fill.js):

'use strict'; 
module.exports = function(){ 
    return{ 
     restrict: 'A', 
     scope: {}, 
     require: '^fillController', 
     link: function (scope, element, attr, ctrl) { 
      var parent = $(element).parents('[fill]').get(0); 
      var config = JSON.parse(attr.fill); 
      ctrl.manageLayout(element, parent, config); 
     } 
    } 
} 

fillController指令看起來像這樣:

'use strict'; 

module.exports = function($window, $timeout){ 
    return { 
     controller: function($scope){ 
      var me = this; 
      me.manageElement = function(child, parent, config){ 
       console.log('managing an element'); 
      } 
     } 
    } 
} 

它需要fillController,但browserify沒有使它可用。我如何得到browserify使這個fillController可用於我的填充指令?我把它們放在正確的順序,這應該是一個同步的東西,爲什麼他們沒有像在同步環境中正確的順序創建?

謝謝!

+0

只是爲了確保我明白了,你在你的HTML中使用這兩個指令,對不對?用'fill'指令作爲'fillController'指令的子項? – SethGunnells

+0

嗨。 fillController指令不會出現在我的html中。 – Liviu

回答

0

您必須在您的HTML中使用這兩個指令才能按照您的意圖工作the require option。我推薦這樣的東西。

HTML:

<div fill fill-controller></div> 
<!-- OR --> 
<div fill-controller> 
    <div fill></div> 
</div> 

fill.js:

// ... 
// This will instruct the directive to look for the fillController directive 
// on the same element the fill directive is on or a parent of it 
require: '^fillController', 
// ... 
相關問題