我正在使用Angular JS(版本1.5.9)和Angular UI路由器(版本1.0.0-rc.1)。
当我尝试访问组件控制器中的绑定值时,它显示describe('checks hello',function(){
beforeEach(inject(_$controller_){
var controller=_$controller_;
};
it('the spec',function(){
spyOn(controller,'test')l
expect(controller.test).toHaveBeenCalled();
})
})
。下面我分享了我的代码。
我的代码有什么问题?你能指导我如何解决这个问题吗?
router.config
undefined
组件
$stateProvider.state('app.user-datshboard', {
url: '/dashboard/:user',
views: {
'header@':{
templateUrl : getView('user-navbar')
},
'main@': {
templateUrl: getView('user-dashboard')
},
'footer':{}
},
params: {
user : null
},
resolve: {
OurUser : function(){
return 'nihao';
}
}
}
})
错误屏幕
class UserDashboardController{
constructor(API,$log,$state,$stateParams,$scope){
'ngInject';
//
$log.log(this);
this.API = API;
this.$scope = $scope;
this.$log = $log;
this.$timeout = $timeout;
this.$state = $state;
this.$stateParams = $stateParams;
this.$log.log(this.OurUser);
}
}
export const UserDashboardComponent = {
templateUrl: './views/app/components/user-dashboard/user- dashboard.component.html',
controller: UserDashboardController,
controllerAs: 'vm',
bindings: { OurUser : '=' }
}
谢谢
答案 0 :(得分:0)
您需要在任何控制器或服务中注入OurUser
,例如:
constructor(API, $log, $state, $stateParams, $scope, OurUser){
// here you have access to OurUser
}
这样您就不需要bindings: { OurUser : '=' }