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

Angular.js Yeoman app.js file not running custom function

本文关键字:js 运行 自定义函数 app Yeoman Angular 文件      更新时间:2023-09-26

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

one@demo~/nod_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();
    }
});

one@demo~/nod_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 $ 

one@demo~/nod_stuff$grunt服务

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...

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

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

另一个你可能做到这一点的地方是在工厂里。由于工厂是Angular中的单体,它也将在引导过程中运行。不同之处在于,工厂初始化的顺序是不确定的,但如果您想保留稍后使用的执行结果,这是很好的。

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