Angularjs 当我使用ui路由器时,我能为多个子视图定义一个控制器吗?
我为ui路由器定义了以下状态:Angularjs 当我使用ui路由器时,我能为多个子视图定义一个控制器吗?,angularjs,Angularjs,我为ui路由器定义了以下状态: var questions = { name: 'questions', url: '/questions', views: { 'menu': { templateUrl: '/Content/app/questions/partials/menu.html', }, 'content': {
var questions = {
name: 'questions',
url: '/questions',
views: {
'menu': {
templateUrl: '/Content/app/questions/partials/menu.html',
},
'content': {
templateUrl: '/Content/app/common/partials/empty.html',
},
'status@': {
templateUrl: '/Content/app/questions/partials/status.html',
}
}
}
var questionsContent = {
name: 'questions.content',
parent: 'questions',
url: '/:content',
views: {
'content@': {
templateUrl: function (stateParams) {
var isNumber = !isNaN(parseFloat(stateParams.content));
return isNumber ? '/Content/app/questions/partials/detail.html' :
'/Content/app/questions/partials/content.html'
},
},
}
}
我只想为此定义一个控制器
我看到过这样的例子,它们对每个视图都适用:
'content@': {
templateUrl: '/Content/app/home/partials/content.html',
controller: 'HomeContentController',
}
我是否有可能拥有一个用于所有设备的控制器
视图,要做到这一点,我需要每次为每个视图指定控制器
视图
另外,当/:content值更改时,如何检测控制器中的状态更改
然后做些什么?我不知道有什么内置的方法可以监视状态的变化,但是你可以使用一个包装了
$state的LocationService
function LocationService($http, $state) {
this.transitionTo = function ($scope, state) {
$state.transitionTo(state);
$scope.$root.$broadcast('stateTransition', state);
};
}
myModule.service('LocationService', ['$http', '$state', LocationService]);
然后,在视图中使用ng click=“goto('root.somestate')”
而不是使用带有href=“#root.somestate”
的锚定
$scope.$watch('stateTransition', function(state) {
console.log('transitioning to ' + state);
});
$scope.goto = function(state) {
locationService.transitionTo($scope, state);
}
在您的控制器中。我不知道有什么内置的方式可以监视状态变化,但您可以使用一个包装了$state的LocationService
。转换到
并从那里广播:
function LocationService($http, $state) {
this.transitionTo = function ($scope, state) {
$state.transitionTo(state);
$scope.$root.$broadcast('stateTransition', state);
};
}
myModule.service('LocationService', ['$http', '$state', LocationService]);
I think you should try following code.
$stateProvider
.state('report',{
views: {
'filters': {
templateUrl: 'report-filters.html',
controller: function($scope){ ... controller stuff just for filters view ... }
},
'tabledata': {`enter code here`
templateUrl: 'report-table.html',`enter code here`
controller: function($scope){ ... controller stuff just for tabledata view ... }
},
'graph': {
templateUrl: 'report-graph.html',
controller: function($scope){ ... controller stuff just for graph view ... }
},
}
})
然后,在视图中使用ng click=“goto('root.somestate')”
而不是使用带有href=“#root.somestate”
的锚定
$scope.$watch('stateTransition', function(state) {
console.log('transitioning to ' + state);
});
$scope.goto = function(state) {
locationService.transitionTo($scope, state);
}
在控制器中。是的,您可以。请阅读感谢链接,但我没有发现任何检测状态变化的方法。怎么做?是的,你可以。请阅读感谢链接,但我没有发现任何检测状态变化的方法。如何做到这一点?
I think you should try following code.
$stateProvider
.state('report',{
views: {
'filters': {
templateUrl: 'report-filters.html',
controller: function($scope){ ... controller stuff just for filters view ... }
},
'tabledata': {`enter code here`
templateUrl: 'report-table.html',`enter code here`
controller: function($scope){ ... controller stuff just for tabledata view ... }
},
'graph': {
templateUrl: 'report-graph.html',
controller: function($scope){ ... controller stuff just for graph view ... }
},
}
})