1

I have two views(index1.html and index2.html). In index1, there is a button that pass two parameters into my controller, I need to set these two variables in my scope that index2.html can access them and by clicking on that button the page goes to index2.html,

here is my code:

in Index1.html:

<button type="button" ng-controller="myController" data-ng-click="detailed('title1', 'url1')" class="btn btn-default">test</button>

controller:

app.controller('myController', function ($scope,$filter,$location,$routeParams, myService) {
    $scope.detailed = function(title,url){
        $location.path('/view2');
        return function(){
            $scope.todos = [
                {text: '@title', url: 'url'},
            ];
        }
    };
});

and I get to my index2.html, but nothing gets generated

<div ng-controller="myController">
    <ul>
        <li ng-repeat="todo in todos">
            <span>{{todo.text}}</span>
        </li>
    </ul>
</div>
Krzysztof Safjanowski
  • 7,292
  • 3
  • 35
  • 47
user1429595
  • 2,635
  • 9
  • 25
  • 30
  • 5
    Please, read about services, and how they help to sharing content between controllers, e.g. :http://stackoverflow.com/questions/21919962/angular-share-data-between-controllers – Krzysztof Safjanowski Apr 19 '15 at 21:51
  • I don't know how to do that, but I can suggest alternative ways to achieve your goal. You can use LocalStorage or Session or $rootScope. All the options are perfectly fine – Adem İlhan Apr 19 '15 at 22:11
  • would you happen to have an example? – user1429595 Apr 19 '15 at 22:30
  • @user1429595 http://stackoverflow.com/questions/21919962/angular-share-data-between-controllers answer with 111 points is not enough good example? Your Todo list should be stored in service - and those service should be injected to controllers where you need this list. – Krzysztof Safjanowski Apr 19 '15 at 23:42
  • The link @KrzysztofSafjanowski pointed you to contains a great example. A good rule of thumb: Whenever you're tempted to share one controller between multiple views...don't! Instead setup a Service that can be injected into multiple controllers so they can share data. – MattDionis Apr 20 '15 at 00:16

1 Answers1

0

A nice alternative is to use $routeParams in combination with $location.

app.controller('Index1Controller', function ($scope, $location) {
  $scope.onClick = function () {
   $location.path('index2').search({data: content});
  };
});

app.controller('Index2Controller', function ($scope, $routeParams) {
  console.log($routeParams.data);
});

This will pass data through your URL which will also keep state when you refresh the page. Sites are supposed to capture state via URL, so you can share URLs with people

Max
  • 1,526
  • 2
  • 16
  • 19