我正在使用ES6类构建AngularJS应用程序,并将跟踪器转换为AMD格式的ES5.
在我的模块中,我导入拦截器类并将其注册为服务,然后使用module.config中的$ httpProvider.interceptors注册此服务:
var commonModule = angular.module(moduleName, [constants.name]);
import authenticationInterceptor from './authentication/authentication.interceptor';
commonModule.service('authenticationInterceptor', authenticationInterceptor);
commonModule.config( $httpProvider => {
$httpProvider.interceptors.push('authenticationInterceptor');
});
Run Code Online (Sandbox Code Playgroud)
我的拦截器类注入$ q和$ window服务,将它们保存在构造函数中供以后使用.我使用调试器跟踪了这一部分并正确地进行了注入:
'use strict';
/*jshint esnext: true */
var authenticationInterceptor = class AuthenticationInterceptor {
/* ngInject */
constructor($q, $window) {
this.$q = $q;
this.$window = $window;
}
responseError(rejection) {
var authToken = rejection.config.headers.Authorization;
if (rejection.status === 401 && !authToken) {
let authentication_url = rejection.data.errors[0].data.authenticationUrl;
this.$window.location.replace(authentication_url);
return this.$q.defer(rejection);
}
return this.$q.reject(rejections);
}
}
authenticationInterceptor.$inject = ['$q', …Run Code Online (Sandbox Code Playgroud) 我好用
<md-sidenav md-component-id="leftNav" md-is-open="vm.isOpen"
md-is-locked-open="vm.isPinned" ... >
<i class="fa fa-bars float-right" ng-click="vm.toggleNav()"></i>
...
</md-sidenav>
Run Code Online (Sandbox Code Playgroud)
使用控制器中的toggleNave方法设置值
class MyController {
constructor($mdSideNav) {
this.mdSideNav = mdSideNav;
this.isPinned = false;
this.isOpen = false;
}
toggleNav = (navId) => {
this.$mdSideNav(navId).toggle().then( () => {
this.isPinned = this.isOpen;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但这会导致屏幕在打开和关闭时闪烁,这似乎不是打开sideNav的最佳方式.
希望是打开/关闭中殿拨动开关,但在我关闭之前保持开放状态.
如果我使用md-is-locked ="$ media('')"那么sideNav锁定打开并且永远不会关闭,直到窗口收缩到media-size以下.这不是我想要的.
如果我根本不使用md-is-locked,那么当你点击它时,sideNav就会关闭,但我希望它保持开放状态!直到我想关闭它.
有人知道怎么做吗?
谢谢
顺便说一句:我使用的是ES6,因此有类和箭头功能.:-)