Adn*_*Ali 6 javascript angularjs angular-ui angularjs-scope
我是angularJS的新手.那么需要有关如何在角度项目中添加全局函数的帮助?加载我的角度应用程序的文件是使用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!");
};
}
});
Run Code Online (Sandbox Code Playgroud)
我需要在不同的视图中访问showScrollAndGo.所以试图让它全球化.知道我做错了吗?
我在这个视图中使用它:
<a href="#/entity/clients/list" data-ng-click="showScrollAndGo('aaa');"><i class="fa fa-circle mrm mlm"></i>Manage Clients</a>
Run Code Online (Sandbox Code Playgroud)
参考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
});
Run Code Online (Sandbox Code Playgroud)
您需要在运行阶段配置rootScope
app.run(function($rootScope) {
$rootScope.showScrollAndGo = function(path) {
alert("I'm global foo!");
};
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15633 次 |
| 最近记录: |