2014-04-22 34 views
0

我有一個yeoman生成器的angular.js安裝。對於express.js,我的印象是,當node.js服務器啓動時(使用grunt服務),app.js文件被使用/讀取以運行函數。然而,在我的應用程序/腳本/ app.js我的功能rest.runApi();沒有運行。爲什麼是這樣?我怎樣才能使這個功能運行,因爲它應該在app.js?服務器啓動時,有輸出沒有錯誤:Angular.js Yeoman app.js文件沒有運行自定義函數

一個@演示〜/ node_stuff $貓的應用程序/腳本/ 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(); 
    } 
}); 

一個@演示〜/ node_stuff $貓的應用程序/腳本/內存。 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; 
[email protected] ~/node_stuff $ 

一個@演示〜/ node_stuff $咕嚕服務

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

當角應用進行引導,它爲鄰在其框架內運行代碼。爲了在加載/引導過程開始時運行它,可以將它放在應用程序的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(); 
});