2013-04-21 72 views
5

在研究了各種不同的項目並閱讀了我能處理的文檔之後,我遇到了如何在我的應用中包含指令的問題。該應用程序是設置如下所示:Angular - 指令和使用模塊

app.js - 只是頂部

angular.module('ngDashboard', ['ngCookies','ngResource','ngDashboard.filters', 'ngDashboard.services', 'ngDashboard.directives']) 

模塊的所有工作,除了精(它是從一個示例重寫的應用程序)的指令,其不要ŧ在所有的工作:

directives.js - 下不工作,在視圖不執行該指令:

angular.module('ngDashboard.directives', []). 
    directive('funkyElement', function() { 
    return { 
     restrict: 'E', 
     transclude: true, 
     scope: 'isolate', 
     template: '<div>gonna parse this: {{orig}} <br/>... and get this: {{obj}}</div>', 
     //templateUrl: 'template.html', 
     compile:function (element, attr, transclusionFunc) { 
      return function (scope, iterStartElement, attr) { 
       var origElem = transclusionFunc(scope); 
       var content = origElem.text(); 
       scope.orig = content; 
       scope.obj = my_custom_parsing(content); 
      }; 
     } 
    }; 
}); 

在同一directives.js文件以下不正常工作,指令執行:

angular.module('ng').directive('funkyElement', function() { 
    return { 
     restrict: 'E', 
     transclude: true, 
     scope: 'isolate', 
     template: '<div>gonna parse this: {{orig}} <br/>... and get this: {{obj}}</div>', 
     //templateUrl: 'template.html', 
     compile:function (element, attr, transclusionFunc) { 
      return function (scope, iterStartElement, attr) { 
       var origElem = transclusionFunc(scope); 
       var content = origElem.text(); 
       scope.orig = content; 
       scope.obj = my_custom_parsing(content); 
      }; 
     } 
    }; 
}); 

的HTML很簡單:

<funky-element> 
    Parse me... come ooooon! Just parse meee! 
</funky-element> 

我的問題是,什麼是包括一組指令,或許爲什麼有道的第一個例子(使用ngDashboard.services)不起作用。

+0

您可以發送帶有實時代碼的重新啓動程序嗎?如果沒有看到更多的代碼,就不可能說出正在發生的事情。根據我目前可以看到的內容,我推測你並沒有使用ng-app =「ngDashboard」初始化你的應用程序,或者在包含文件時混淆了事物。現場代碼再一次清楚地顯示了這一切。 – 2013-04-21 16:57:30

+0

我會發布一些實時代碼,但整個應用程序工作正常,所有的服務,控制器,唯一不能正常工作的是指令。 – lucuma 2013-04-21 17:05:49

+0

它既可以是app.js文件被緩存,也可以在添加指令依賴項後保存。經過這麼長時間的搞亂之後,我懷疑它沒有得救,但我對這種可能性持開放態度。 – lucuma 2013-04-21 18:43:52

回答

5

事實證明,我所使用的app.js文件不是緩存的,所以指令依賴關係不存在,或者我忽略了保存它(兩個都可能在週末工作和深夜)。自後,我進行了更新app.js我會記住這這個問題被固定爲解決與建議:

  1. 檢查腳本控制檯,以確保您的文件不會被緩存
  2. 關閉完全緩存或使用隱身模式。
  3. 始終確保NG-應用程序添加到您的文檔(情況並非如此,但能幫助別人)
  4. ,請務必保存您的文件
  5. 喝更多的咖啡,當你累了,學習新編程語言/框架。

最後,關於Angular,我還沒有意識到你可以添加指令到ng模塊,他們會變得可用。我確信這不是一個最佳實踐,但是爲了測試和組合快速代碼,它可能派上用場。