【发布时间】:2015-02-24 00:57:35
【问题描述】:
我正在尝试使用 AngularJS 创建一个 CRUD 页面,我使用 Angular Router UI 进行页面更改
如何在路由的controller 中访问主Controller 中的变量?
//This is my main controller:
var app = angular.module('app', [
'ui.router'
]);
app.controller('MyCtrl', function ($scope) {
$scope.persons = [
{name: 'Daisy', description: 'Developer'},
{name: 'Zelda', description: 'Developer'},
{name: 'Luide', description: 'Developer'},
{name: 'Mario', description: 'Developer'}
];
});
这是我的路由器配置 app.config(function ($stateProvider, $urlRouterProvider) { $urlRouterProvider.otherwise('/');
$stateProvider
.state('list', {
url: '/list',
templateUrl: 'list.html',
controller: 'MyCtrl'
})
.state('edit', {
url: '/edit',
templateUrl: 'edit.html',
controller: function($scope, $state) {
$scope.person = {name: "", description: ""};
$scope.click = function () {
$state.go('list');
};
}
})
.state('add', {
url: '/add',
templateUrl: 'edit.html',
controller: function($scope, $state) {
$scope.person = {name: "", description: ""};
$scope.click = function () {
// HOW TO CAN I ACCESS $SCOPE.PERSONS FOR MAKE THIS PUSH?
$scope.persons.push($scope.person);
$state.go('list');
};
}
});
});
【问题讨论】:
-
如果您将
edit和add设为list的子路由,那么您只需将persons设为解析即可。然后所有子路由都可以注入它。
标签: javascript angularjs controller angular-ui-router