Angularjs:如何通过控制器将Scope变量从视图传递到另一个视图?

时间:2015-04-19 21:45:40

标签: javascript angularjs angularjs-scope

我有两个视图(index1.html和index2.html)。在index1中,有一个按钮将两个参数传递给我的控制器,我需要在我的范围内设置这两个变量,index2.html可以访问它们,点击该按钮,页面转到index2.html,

这是我的代码:

在Index1.html中

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

控制器:

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'},
            ];
        }
    };
});

我到了我的index2.html,但没有生成任何内容

<div ng-controller="myController">
    <ul>
        <li ng-repeat="todo in todos">
            <span>{{todo.text}}</span>
        </li>
    </ul>
</div>

1 个答案:

答案 0 :(得分:0)

一个不错的选择是将$routeParams$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);
});

这将通过您的URL传递数据,这也会在您刷新页面时保持状态。 网站应该通过网址捕获状态,因此您可以与人共享网址