简体   繁体   English

应用程序基础:为 angularjs 控制器、服务等创建单独的文件

[英]Foundation for apps: Creating separate files for angularjs controllers, services etc

I just got started with Foundation for Apps, but I was having trouble adding my angular controllers in a separate folder and using them.我刚刚开始使用 Foundation for Apps,但是在将我的 angular 控制器添加到单独的文件夹中并使用它们时遇到了问题。

I have this current structure in my assets' js folder :我的资产的 js 文件夹中有这个当前结构:

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

but I want it to be但我希望它是

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

I don't want to keep a single large js file developed by chaining my controllers, services etc. I want a more modular structure as specified.我不想保留通过链接我的控制器、服务等开发的单个大型 js 文件。我想要一个更模块化的结构。

When I changed it to the modular structure, I got following three errors:当我将其更改为模块化结构时,出现以下三个错误:

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

Here's my template:这是我的模板:

---
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: home.controller.js:

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

app.js:应用程序.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);
}

To solve this I tried adding my controller reference in gulpfile.js by referring this现参照解决这个我试过gulpfile.js加入我的控制器参考

I also referred this but I still cannot get it to work.我也提到了这个,但我仍然无法让它工作。 Any idea what I am doing wrong?知道我做错了什么吗?

Inside gulpfile.js check this line .gulpfile.js 中检查这一行 You'll see that only client/assets/js/app.js is used when coupling your minimized file.您将看到在耦合最小化文件时仅使用了client/assets/js/app.js You need to make it copy all your controller 's .js files too :您还需要让它复制所有控制器.js文件:

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

You can also use an angular module to hold all your controllers & services then injecting it to your app module.您还可以使用 angular 模块来保存所有控制器和服务,然后将其注入您的应用程序模块。 This is how I'm using it here and this is how it is also done in this great ready to use Fork : https://github.com/CreativityKills/foundation-apps-template这就是我在这里使用它的方式,这也是它在这个随时可用的 Fork 中的使用方式: https : //github.com/CreativityKills/foundation-apps-template

  • Note: remember to restart gulp after each modify of its config file and check if your code has been well included in the built JS file ( by default is /build/assets/js/app.js )注意:每次修改配置文件后记得重启 gulp 并检查你的代码是否已经很好地包含在构建的 JS 文件中(默认为/build/assets/js/app.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM