Angularjs - 更新模型和位置后未更新第二个视图

时间:2014-07-26 19:06:31

标签: angularjs angularjs-scope angular-routing

我有一个管理两个视图的控制器,我想根据第一个视图中选择的条目更新第二个视图。

==>第一种观点:stores.html:

 <div>
    <ol class="breadcrumb" ng-repeat="store in stores">
        <li>
            {{store.Name}} &nbsp;&nbsp;
            <a href="javascript:void(0)" ng-click="selectStore(store.Index)">
                {{store.Alerts.length}} Alerts
            </a>
        </li>
    </ol>
</div>

==&GT;第二个视图store-selected.html:

<div>{{message}}</div>

<div>
    {{selectedStore.Name}}
</div>

==&GT;角度路线:

.when('/store', {
                controller: 'StoreCtrl',
                templateUrl: 'views/store/stores.html'
            })
            .when('/store-selected', {
                controller: 'StoreCtrl',
                templateUrl: 'views/store/store-selected.html'
            })

==&GT;角度控制器:

.controller('StoreCtrl', ['$scope', '$location', function ($scope, $location) {
            $scope.message = "Init message";

            $scope.stores = [
                { Index: 0, Name: 'Store A', Alerts: [{ id: 1 }] },
                { Index: 1, Name: 'Store B', Alerts: [{ id: 2 }, { id: 3 }] },
                { Index: 2, Name: 'Store C', Alerts: [{ id: 4 }] }
            ];

            $scope.selectedStore = $scope.stores[0];

            $scope.selectStore = function (storeIndex) {
                $scope.message = "click event called!";
                $scope.selectedStore = $scope.stores[storeIndex];

                $location.path('/store-selected');

            };
        }])

显示第二个视图,但模型的更新未反映到其中。

有人可以告诉我我做错了吗?

提前多多感谢!

1 个答案:

答案 0 :(得分:0)

您想要使用此类路线$routeParams

.when('store:storeId', {
    controller: 'StoreCtrl',
    templateUrl: 'views/store/store-selected.html'
})

然后在ctrl

.controller('StoreCtrl', function ($scope, $routeParams) {
    $routeParams.storeId;
});

在单个UI上执行此操作,您可能需要使用angular-ui-router或其他内容。