使两个布局共享相同的 $scope

Make two layouts share the same $scope

我想为我的内容提出两种布局(即 horizontalvertical)。因此切换选择器将自动导致相应的布局。这是 JSBin:

<html ng-app="flapperNews">
<head>
    <script src="https://code.jquery.com/jquery.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.3.2/angular-ui-router.js"></script>
    <script type="text/ng-template" id="horizontal.tpl">
        <textarea ng-model="one"></textarea>, <textarea ng-model="two"></textarea>
        <br><br>{{one}}+{{two}}
    </script>
    <script type="text/ng-template" id="vertical.tpl">
        <textarea ng-model="one"></textarea><br><textarea ng-model="two"></textarea>
        <br><br>{{one}}+{{two}}
    </script>
    <script>
    var app = angular.module('flapperNews', ['ui.router']);

    app.config(['$stateProvider', function ($stateProvider) {
        $stateProvider
            .state('entry', {
                url: '/',
                params: { tpl: 'vertical' },
                templateUrl: function (params) {
                  return params.tpl + ".tpl"
                }
            })
    }]);

    app.controller('MainCtrl', ['$scope', '$state', function ($scope, $state) {
        $scope.one = "one";
        $scope.two = "two";
        $scope.layouts = ["horizontal", "vertical"];
        $scope.$watch('layout', function () {
          $state.go('entry', {tpl: $scope.layout});
        })
    }])
    </script>
</head>

<body ng-controller="MainCtrl">
    <select ng-model="layout" ng-init="layout = 'horizontal' || layouts[0].value" ng-options="x for x in layouts"></select>
    <br><br>
    <ui-view></ui-view>
</body>

</html>

但是,使用上面的代码,每次我们更改视图时,$scope.one$scope.two 都会重置为它们的初始值。我希望无论布局如何变化,他们的文本区域的变化都会保留。

有人知道怎么解决吗?

我认为您应该使用 nested 视图 - 您可以在父路由状态上定义主控制器,并定义对应于两个视图的两个嵌套状态。这样,父控制器将保留(切换子状态时不会 re-initialised )并且只会更改嵌套状态视图。像这样:

$stateProvider
  .state('myState', {
    url: '/test/',
    template: '<div ui-view></div>',
    controller: function() {
        //main controller shared by child states
        ...
    }
  })
  .state('myState.view1', {
    url: '/test/view1'
    templateUrl: 'tpl-1.hmtl',
    ...
  })
  .state('myState.view2', {
    url: '/test/view2'
    templateUrl: 'tpl-2.hmtl',
    ...
  })

使用 factories 轻松在不同视图之间共享相同数据(AngularJS factory documentation). Try this example,它使用名为 myFactory 的简单工厂在控制器之间共享数据。这也适用于相同的与您的情况一样的控制器。

var myApp = angular.module("myApp",[ "ui.router"]);

myApp.config(function ($stateProvider, $urlRouterProvider){
    $stateProvider.state("state1", {
        url: "#",
        template: '<p>{{ aValue }}</p><button ng-click="bindValue(\'its me\')">Bind value</button>',
        controller: "myController"
    }).state("state2", {
        url: "#",
        template: '<p>{{ aValue }}</p><button ng-click="bindValue(\'its me\')">Bind value</button>',
        controller: "myController"
    });
});


myApp.controller( "myController", function($scope, myFactory) {
    $scope.aValue = myFactory.myValue;

    $scope.bindValue = function (value) {
        $scope.aValue = value;
        myFactory.myValue = value;
    }
});


myApp.factory('myFactory', function () {
    return {
        myValue: ''
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.8/angular-ui-router.min.js"></script>
<div ng-app="myApp">
    <nav>
        <a ui-sref="state1">State 1</a>
        <a ui-sref="state2">State 2</a>
    </nav>
    <div ui-view></div>
</div>