2015-09-09 56 views
2

我剛剛開始使用Foundation for Apps,但我遇到了問題將角度控制器添加到單獨的文件夾中並使用它們。應用程序的基礎:爲angularjs控制器,服務等創建單獨的文件

我有這樣的現有結構在我資產的JS文件夾

js/ 
    app.js //this has all the controllers etc. by chaining 

,但我希望它是

js/ 
    app.js 
    controllers/ 
     home.controller.js 
     main.controller.js 

我不想讓一個大的js文件通過鏈接我的控制器,服務等來開發。我想要一個更具模塊化結構的指定。

當我把它改成了模塊化結構,我得到了以下三個錯誤:

1. 'HomeCtrl' not defined. 
2. Uncaught SyntaxError: Unexpected token < at the 1st line of home.controller.js 
3. Resource interpreted as Script but transferred with MIME type text/html: "http://localhost:8079/assets/js/home.controller.js". at the point where I am including 'home.controller.js' in index.html 

這裏是我的模板:

--- 
name: home 
url:/
controller: HomeCtrl 
--- 

<div class="grid-container"> 
    <h1>Welcome to Foundation for Apps!</h1> 
    <p class="lead">This is version <strong>1.1 Weisshorn</strong>.</p> 
</div> 

home.controller.js:

app.controller('HomeCtrl', ['$scope', function($scope) { 
    $scope.temp = 'hello'; 
}]); 

app.js:

var app = angular.module('application', [ 
    'ui.router', 
    'ngAnimate', 
    'foundation', 
    'foundation.dynamicRouting', 
    'foundation.dynamicRouting.animations' 
]) 
    .config(config) 
    .run(run) 
; 

config.$inject = ['$urlRouterProvider', '$locationProvider']; 

function config($urlProvider, $locationProvider) { 
    $urlProvider.otherwise('/'); 

    $locationProvider.html5Mode({ 
    enabled:false, 
    requireBase: false 
    }); 
} 

function run() { 
    FastClick.attach(document.body); 
} 

爲了解決這個我試過參照添加在gulpfile.js我控制器參考this

我還提到this,但我仍然無法得到它的工作。任何想法我做錯了什麼?

回答

1

Inside gulpfile.js檢查this line。您將看到在耦合最小化文件時僅使用client/assets/js/app.js。你需要使它複製所有控制器的.js文件太:

// These files are for your app's JavaScript 
appJS: [ 
    'client/assets/js/app.js', 
    './client/assets/js/controllers/*.js', 
] 

你也可以使用一個角模塊,以承載所有的控制器&服務,那麼它注射到你的應用程序模塊。這是我如何使用它here這是怎麼也正是在這個偉大的準備用叉子做:https://github.com/CreativityKills/foundation-apps-template

  • 注:請記住後的配置文件的每個修改重啓杯和檢查,如果你代碼已經包含在內置的JS文件中(默認爲/build/assets/js/app.js
相關問題