2015-04-06 78 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', 
// ... 
相关问题