[英]Angular 1/Ui-router - Pass data from resolve to controller
I want to pass a data ( contactName
) from the state to the controller by the resolves : 我想通过解析将数据(
contactName
)从状态传递到控制器:
.state({
name: 'contact.detail.overlay',
abstract: true,
component: 'overlayContent',
resolve: {
contactName: (originalContact, $rootScope, ContactService) => {
$rootScope.contactName = ContactService.getContactName(originalContact)
return $rootScope.contactName
}
}
})
on my state all is correct, contactName
is also correct, but it is not accessible in my overlayContent
controller, do you have a solution to have $rootScope.contactName
access in the controller ? 在我的状态下,一切都是正确的,
contactName
也是正确的,但是在我的overlayContent
控制器中无法访问它,您是否有解决方案来在控制器中具有$rootScope.contactName
访问权限?
EDIT : The controller : 编辑:控制器:
class overlayContentComponent {
constructor($state, $interpolate, $scope) {
'ngInject'
this.$scope = $scope
this.$state = $state
this.$interpolate = $interpolate
}
/**
*
* On initialization hook, let's check if an overlay title
* has been defined for the current state.
*/
$onInit() {
const stateData = this.$state.current.data || { title: '' }
this.overlayTitle = angular.isDefined(stateData.title) ? this.$interpolate(stateData.title)() : ''
}
}
export const overlayContentComponent = {
template: require('./overlay-content.html'),
controller: overlayContentComponent
}
I can access contactName
in my controller by this.$scope.$root.contactName
but I would like to avoid $scope.$root
by another alternative 我可以访问
contactName
在我的控制器通过this.$scope.$root.contactName
,但我想,以避免$scope.$root
的另一种选择
Change your constructor: 更改您的构造函数:
constructor($state, $interpolate, $scope, contactName) {
'ngInject';
// The variable you want:
this.contactName = contactName;
this.$scope = $scope
this.$state = $state
this.$interpolate = $interpolate
}
The 'contactName' resolve should be injected now, into the controller for that state (where the resolve resides). 现在应将“ contactName”解析注入该状态(解析所驻留的位置)的控制器中。 This should allow you to access it in the controller, as above.
如上所述,这应该允许您在控制器中访问它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.