I'm trying create a CRUD page, using AngularJS, I using Angular Router UI for pages changes
How to can I access a variable in my main Controller
, in controller
of route?
//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'}
];
});
This is my router config 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
andadd
child routes oflist
, then you can just makepersons
a resolve. All child routes can then inject it. – Mathew Foscarini Dec 26 '14 at 20:53