Search code examples
javascriptangularjsangularjs-scopeviewstateionic-framework

Choose element Angular


I have problem using Ionic with Angular JS. I get list using Ajax but I don't know how send it.

 <li ng-repeat=" list in list">
        <a class="item item-icon-right " ng-click='next({list})'>
            <i class='icon ion-chevron-right'></i>
            <p>{{list.rejestracja}}, {{list.auto}}</p>
            <p>{{list.klient}}</p>
        </a>
    </li>

I would like to get another view only those elements that I chose from this list.

Ok sorry. When you choose one item from the list, I would like to show it in the next view. May show more:

And in my controller I have the following function:

$scope.next = function(message) {
    $scope.user = message;
    console.log($scope.user);
    $state.go('master');
} 

So when I go to 'master' view I can't use this $scope.user, it doesn't show me it in view. Does someone know how change it?


Solution

  • ionic uses ui-router so if your next view is a nested view you should be able to use your controllers variables in a child controller through scope inheritance (https://github.com/angular-ui/ui-router/wiki/Nested-States-%26-Nested-Views). if your view is not nested, then you have some options

    1-put it in $rootScope
    2-use session/local storage
    3-create a service to hold that information across the application
    

    the way to do it depends on the additional functionality and livetime of the information if passing that data, is all there is ever going to happen you could use $rootScope or creating a clipboard service that holds all those peaces of random data

    using $rootScope in your controllers inject $rootScope in both the one with the data source and the one owning the view you want to display the data on. then in your source controllers next function

    $scope.next = function(message) {
      $scope.user = message;
      $rootScope.user=message;
      console.log($scope.user);
      $state.go('master');
    } 
    

    then in your destination controller do

    $scope.message=$rootScope.message;
    

    that should give you access to the message in your view.

    this is not the cleanest way, but it will allow you to bypass this problem and not get you frustrated, you can revisit this an implement a better solution when you fell like it