Angular.js Yeoman app.js文件没有运行自定义函数

时间:2014-04-22 01:35:47

标签: javascript node.js angularjs

我有一个来自yeoman发电机的angular.js安装。对于express.js,我的印象是当node.js服务器启动时(使用grunt serve),使用/读取app.js文件来运行函数。但是,在我的app / scripts / app.js中我的函数rest.runApi();没有运行。为什么是这样?如何使该功能在app.js中运行?服务器启动时输出没有错误:

一个@ demo~ / node_stuff $ cat app / scripts / app.js

'use strict';

var rest = require('./memory.js');

rest.runApi();

var myappApp = angular
  .module('myappApp', [
    'ngCookies', 'duScroll', 'ngRoute'
  ]).config(function($routeProvider) {
      $routeProvider.when('/home', {
      templateUrl: 'views/home.html',
      controller: 'HomeController'
      });

      $routeProvider.when('/first', {
      templateUrl: 'views/first.html',
      controller: 'FirstController'
      });



      $routeProvider.otherwise({ redirectTo: '/home'});
  });

myappApp.controller('Scroller', function($scope, $location, $document, $anchorScroll) {
    $scope.scrollTo = function(id) {
    var el = angular.element(document.getElementById(id));
    $document.scrollTo(el, 2000);
    }
});

myappApp.controller('HomeController', function($scope, $location, $anchorScroll) {
    $scope.scrollTo = function(id) {
    $location.hash(id);
    $anchorScroll();
    }
});

myappApp.controller('FirstController', function($scope, $location, $anchorScroll) {
    $scope.scrollTo = function(id) {
    $location.hash(id);
    $anchorScroll();
    }
});

一个@ demo~ / node_stuff $ cat app / scripts / memory.js

var rest = require('restler');

CHECK='chMNWXNki7';
URL='https://monitoring.api.rackspacecloud.com/v1.0/1324/';
SLUG='entities/enij5mKIvs/checks/';
MODE='/test';

url = CHECK+URL+SLUG+CHECK+MODE
headers = { headers: 
    { "X-Auth-Token": "377c" }
}

function runApi() {
    rest.post(url, headers).on('complete', function(data, response) {
            console.log(JSON.stringify(data, "\n", 2));
            console.log("--------------------------------------------------------");
            console.log(response.statusCode);
        });
}

module.exports.runApi = runApi;
one@demo ~/node_stuff $ 

一个@ demo~ / node_stuff $ grunt serve

Running "serve" task

Running "clean:server" (clean) task
Cleaning .tmp...OK

Running "bowerInstall:app" (bowerInstall) task

Running "concurrent:server" (concurrent) task

    Running "copy:styles" (copy) task
    Copied 1 files

    Done, without errors.


    Execution Time (2014-04-22 01:20:32 UTC)
    loading tasks   4ms  âââââââââââââ 27%
    copy:styles    10ms  ââââââââââââââââââââââââââââââââ 67%
    Total 15ms

Running "autoprefixer:dist" (autoprefixer) task
Prefixed file ".tmp/styles/main.css" created.

Running "connect:livereload" (connect) task
Started connect web server on 0.0.0.0:9000.

Running "watch" task
Waiting...

1 个答案:

答案 0 :(得分:1)

当Angular应用程序执行引导时,它只在其框架内运行代码。为了在加载/引导过程的最开始时运行它,您可以将它放在应用程序的run方法中。

myappApp.run(function apprun() {
  var rest = require('./memory.js');
  rest.runApi();
});

您可以做到这一点的其他地方是在工厂内。由于工厂是Angular的单身人士,它也将在引导期间运行。不同之处在于工厂初始化的顺序是不确定的,但是如果你想保持执行结果以后再使用,那就好了。

myApp.factory('RestRunFactory', function factoryrun() {
  var rest = require('./memory.js');
  rest.runApi();
});