Shi*_*abu 17 html javascript jquery global-variables angularjs
我刚刚创建了一个angularJS应用程序.
这是我的index.html
<html ng-app="MyApp">
<head>
<!-- CSS files import -->
</head>
<body class="{{bodylayout}}">
<div ng-view></div>
</body>
<--! JS imports
aungular.js
app.js
login.js
register.js
-->
</html>
Run Code Online (Sandbox Code Playgroud)
app.js
'use strict';
//Define Routing for app
angular.module('myApp', []).config(['$routeProvider', '$locationProvider',
function($routeProvider,$locationProvider) {
$routeProvider
.when('/login', {
templateUrl: 'login.html',
controller: 'LoginController'
})
.when('/register', {
templateUrl: 'register.html',
controller: 'RegisterController'
})
.when('/forgotPassword', {
templateUrl: 'forgotpassword.html',
controller: 'forgotController'
})
.when('/home', {
templateUrl: 'views/home.html',
})
.otherwise({
redirectTo: '/login'
});
// $locationProvider.html5Mode(true); //Remove the '#' from URL.
}]);
Run Code Online (Sandbox Code Playgroud)
我有login.html,register.html和forgetpassword.html,home.html.每个人都在单独的文件中有独立的控制器.login.js,register.js,forgot.js,home.js.
login.js
'use strict';
angular.module('myApp').controller('LoginController', function($scope, $location, $window) {
$scope.user = {};
$scope.loginUser=function()
{
var username=$scope.user.name;
var password=$scope.user.password;
if(username=="admin" && password=="admin123")
{
$location.path( "/home" );
}
else
{
$scope.message="Error";
$scope.messagecolor="alert alert-danger";
}
}
});
Run Code Online (Sandbox Code Playgroud)
同样,我在其他控制器中有post方法.
我想要的是,当视图是登录或注册或忘记密码时,正文类应该是"login-layout".所以在身体我说class="{{bodylayout}}".我知道使用全局变量,值可以设置.但我不知道如何.
在app.js我试过这个
angular.module('myApp').factory("myService", function(){
return {
sharedObject: { data: 'login-layout' }
};
});
Run Code Online (Sandbox Code Playgroud)
但不知道如何使用它.
Rez*_*eza 19
您可以通过两种方式创建全局变量
使用$rootScope你可以在你的LoginController控制器中做一些事情
angular.module('myApp').controller('LoginController', function($scope, $location, $window, $rootScope) {
$scope.user = {};
$rootScope.bodylayout = 'login-layout';
//others code
}
Run Code Online (Sandbox Code Playgroud)
运用 service
angular.module('myApp').factory("myService", function(){
return {
sharedObject: { data: 'login-layout' }
};
});
Run Code Online (Sandbox Code Playgroud)
在控制器中使用此服务
angular.module('myApp').controller('LoginController', function($scope, $location, $window, myService) {
$scope.user = {};
$rootScope.bodylayout = myService.sharedObject.data; // get data from service
//others code
}
Run Code Online (Sandbox Code Playgroud)
你的HTML样子
<body class="{{bodylayout}}">
Run Code Online (Sandbox Code Playgroud)
请注意,在这种情况下,您需要bodylayout在每个控制器中设置,否则它将使用旧值
imc*_*mcg 10
尝试使用$ rootScope:
$rootScope.bodyClass = 'login-layout';
<body class="{{$root.bodyClass}}">
Run Code Online (Sandbox Code Playgroud)
您可以通过监听routeChangeSuccess在各个控制器中处理,也可以在app.js中处理:
$rootScope.$on('$routeChangeSuccess', function (event, currentRoute) {
switch(currentRoute.templateUrl) {
case 'login.html':
case 'register.html':
case 'forgotpassword.html':
$rootScope.bodyClass = 'login-layout';
break;
default:
$rootScope.bodyClass = '';
break;
}
});
Run Code Online (Sandbox Code Playgroud)
您可以创建一个<body>指令,添加和删除可在整个应用程序中触发的类事件.
angular.module('myApp').directive('body', [function(){
return {
restrict: 'E',
link: function(scope, element, attrs) {
scope.$on('body:class:add', function(e, name){
element.addClass(name);
};
scope.$on('body:class:remove', function(e, name){
element.removeClass(name);
};
return;
}
};
}]);
Run Code Online (Sandbox Code Playgroud)
在您的app.js config块中,您可以将其设置<body> class为您想要的任何内容$emit
## Add class
$rootScope.$emit('body:class:add', 'login-layout')
## Remove class
$rootScope.$emit('body:class:remove', 'login-layout')
Run Code Online (Sandbox Code Playgroud)
它只是简单地使用角度jqLite addClass(),removeClass()并且也不要求你$element使用link已经拥有dom访问元素的指令函数.
即使没有$rootScope你可以在你的控制器内调用它$scope.$emit('body:class:add', name).