-1

I use ui-router to route to particular sub page as needed:

var myApp = angular.module("myApp",['ui.router']);
    myApp.config(function($stateProvider, $urlRouterProvider) {

  $stateProvider
  .state('usergroups', {
  url: "/usergroups",
  templateUrl: "pages/usergroups.html",
  controller : 'UsergroupsCtrl'
  })
  .state('usergroups.create', {
  url: "/create",
  templateUrl: "pages/usergroups.new.html",
  controller : 'UsergroupsCtrl'
  })

...

This is my usergroups.html:

  <body>
    <h3>User Groups</h3><br/>

    <button class="fon-btn" type="button" ng-hide = "toAdd" ng-click="toAdd = true"  ui-sref="usergroups.create">Create New Group</button>  
    <div ui-view></div>
      <ul>
        <li class="bitcard padding10" ng-repeat="group in usergroups">
            <span class="padding10"><strong>{{group.name}}</strong></span>
        </li>
        </ul>
  </body>

and usergroups.new.html:

<body>
<form ng-submit="add()">
      <input class="btn" type="submit" value="Add Usergroup"><br/><br/>
      </form>
</body>

When I click on Add Usergroup, it will called add() from UsergroupsCtrl:

$scope.add = function() {
    $scope.usergroups.push({name:$scope.name});
    console.log($scope.usergroups);
    }

The console show that $scope.usergroups is already updated. but somehow in usergroups.html: ng-repeat="group in usergroups" did not update the view. I add $scope.$apply() and $scope.$digest() but got an error:

$apply already in progress

But everything works fine if I did not use ui-router. How can I resolve this?

sooon
  • 4,718
  • 8
  • 63
  • 116
  • Don't think that controller `UsergroupsCtrl` is shared between parent and child route. The controller refreshes on each route change. – Rahil Wazir Nov 26 '15 at 07:29
  • why don't you try creating a service so you can share data among different controllers. – NPToita Nov 26 '15 at 07:43

1 Answers1

0

I think same $scope cannot be used in two different controllers. you can change it to $rootScope. like $rootScope.usergroups.push(). This SO may help you. You can use $stateParam also.

Community
  • 1
  • 1
krish
  • 537
  • 2
  • 14
  • I got an error: `Error: Can't find variable: $stateParam` – sooon Nov 26 '15 at 07:47
  • you have to inject $stateParams and than you can use it. did you go through ui-router doc ? here it is https://github.com/angular-ui/ui-router/ – krish Nov 26 '15 at 07:48