angularJS中的全局函数
Global Functions in angularJS
我是angularJS的新手。所以需要关于如何在angular项目中添加全局函数的帮助吗?文件加载我的angular应用程序使用showScrollAndGo函数,该函数应该作为全局方法工作,但它不工作。app.js的代码为:
'use strict'; define(
[
'angular',
'jQuery',
'angular-route',
'angular-resource',
'angular-ui-bootstrap',
'highcharts',
'highcharts-theme',
'highcharts-ng',
'controllers/index',
'directives/index',
'filters/index',
'services/index',
'angular-token-auth',
'angular-local-storage',
'jquery.slimscroll',
'jquery-icheck'
],
function(angular, $) {
'use strict';
return angular.module('radian', ['ngRoute', 'ngResource', 'ui.bootstrap', 'app.services', 'app.controllers', 'app.directives','app.filters', 'highcharts-ng', 'ng-token-auth', 'ngStorage'])
.constant('globals', {
API_URL: 'http://localhost:3000/api',
AUTH_URL: 'http://radiancor-env.elasticbeanstalk.com',
TEMPLATE_URL: 'app/views'
})
.constant('pagination', {
items_per_page: 10,
max_size: 5,
direction_links: true,
boundary_links: true,
current_page: 1,
total_items: 0
})
.config(['$routeProvider', 'globals', routeConfiguration])
.config(['$httpProvider', httpConfiguration])
.config(['$authProvider', 'globals', authProvider])
.config(['$rootScopeProvider', root_functions])
.config(['paginationConfig', paginationConfiguration]);
function authProvider($authProvider, globals) {
$authProvider.configure({
apiUrl: globals.AUTH_URL
});
}
function paginationConfiguration(paginationConfig) {
paginationConfig.firstText = '<<';
paginationConfig.lastText = '>>';
paginationConfig.nextText = '>';
paginationConfig.previousText = '<';
}
function routeConfiguration($routeProvider, globals) {
$routeProvider
.when('/', {
templateUrl: globals.TEMPLATE_URL+'/misc/login.html',
controller: 'LoginController',
controllerAs: 'login'
})
.when('/dashboard', {
templateUrl: globals.TEMPLATE_URL+'/misc/dashboard.html',
controller: 'DashboardController',
controllerAs: 'dashboard'
})
.when('/entity/:entity/:action', {
templateUrl: function(rp) {
return globals.TEMPLATE_URL+'/'+rp.entity+'/'+rp.action+'.html';
}
})
.when('/entity/:entity/:action/:id', {
templateUrl: function(rp) {
return globals.TEMPLATE_URL+'/'+rp.entity+'/'+rp.action+'.html';
}
})
.otherwise({
redirectTo: '/'
});
}
function httpConfiguration($httpProvider) {
$httpProvider.defaults.headers.common['Content-Type'] = 'application/json; charset=utf-8';
$httpProvider.defaults.headers.common['Accept'] = 'application/json, text/javascript';
$httpProvider.defaults.useXDomain = true;
$httpProvider.interceptors.push('interceptService');
}
function root_functions($rootScope) {
$rootScope.showScrollAndGo = function(path) {
alert("I'm global foo!");
};
}
});
我需要在不同的视图中访问showScrollAndGo。因此,努力使其全球化。知道我哪里做错了吗?
我使用它的视图如下:
<a href="#/entity/clients/list" data-ng-click="showScrollAndGo('aaa');"><i class="fa fa-circle mrm mlm"></i>Manage Clients</a>
参考Angular的文档
angular.module('myModule', []).
config(function(injectables) { // provider-injector
// This is an example of config block.
// You can have as many of these as you want.
// You can only inject Providers (not instances)
// into config blocks.
}).
run(function(injectables) { // instance-injector
// This is an example of a run block.
// You can have as many of these as you want.
// You can only inject instances (not Providers)
// into run blocks
});
你需要像一样在运行阶段配置rootScope
app.run(function($rootScope) {
$rootScope.showScrollAndGo = function(path) {
alert("I'm global foo!");
};
});
您可以尝试添加一个工厂,并在所有需要的控制器中引用该工厂。
样品:
在app.js中添加一个工厂:
app.factory('notificationFactory', function (toaster) {
//.............
});
在任何控制器文件中,您都可以通过名称引用该工厂,并像下面这样使用它,
app.controller('sampleController', ['$scope','notificationFactory',
function ($scope, notificationFactory) {
}
]);
相关文章:
- 正在全局范围中查找JavaScript函数
- 在javascript函数中设置全局变量
- javascript无法重新定义函数内部的全局对象
- javascript 中的全局函数
- cordova:例外:财产'requestFileSystem'的[对象全局]不是函数
- 如何从onclick函数设置全局变量并将其传递给另一个JS文件
- Javascript,从静态函数中打印全局对象
- 全局窗口热键在最小化chrome窗口时调用js函数
- 创建要在其他函数中使用的全局变量
- 从全局函数调用Ember控制器上的方法
- 保留 JQuery Vars 用于另一个函数(全局变量?)
- jQuery-函数全局变量
- jQuery - 如何使这个javascript函数全局化,以便其他函数可以访问它
- JS函数/全局变量的作用域
- Javascript;如何使函数全局可访问
- 全局在什么(函数(全局){一些代码.(这个)做
- 如何使用 (函数(全局) { .. })(这);.
- 我如何使变量newUser定义在一个函数全局
- 函数(全局){..}(this)的工作方式
- 我如何使我的变量包含在这个函数全局