Angular.js Yeoman app.js文件未运行自定义函数
Angular.js Yeoman app.js file not running custom function
我从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();
});
相关文章:
- 如何开发节点.js运行时策略
- 如何仅在通过node.js运行script.js时激发代码
- 如何从Node.js运行gullow任务-gullow.start()的任何更好的替代方案
- 使用节点 JS 运行连续后台作业
- JS运行两次不起作用
- 无法让 JS 运行
- JS - 运行带有变量名的 jQuery 函数
- 向 DOM 添加一个简单的图像标记,而不是使用 GWT 控件以获得更好的 JS 运行时性能
- 使用 express.js 和 node.js 运行我的示例应用
- 如何阻止 js 运行
- 在需要模块之前,请使用require.js运行公共代码
- 使用While True时,使用node js运行python脚本不起作用.keep-listen
- 构造Collection后,让backbone.js运行函数
- JS运行后在DOM中加载的图像
- 从node.JS运行webgl JS文件
- 只有在VS 2010中调试时才出现JS运行时错误
- 让Grunt.js运行node.js模块
- 我可以从JS运行chrome包应用程序
- 通过node.js运行带有参数的Bash脚本
- 打开和关闭Stellar.js运行时