【问题标题】:AngularJs - stateProvider to reloading controller when page refreshAngularJs - stateProvider 在页面刷新时重新加载控制器
【发布时间】:2017-10-05 12:03:27
【问题描述】:

我有一个链接到不同页面和不同控制器的状态,如下所示:

.state('productEdit', {
                url: '/productEdit/:id',
                templateUrl: '/App/Main/views/products/editProduct.cshtml',
                controller: 'app.views.products.editProduct',
                controllerAs: 'vm'
            })

问题是,当我转到此页面或刷新链接页面时,控制器不会再次初始化,因此即使我清理缓存,变量也会“卡住”。

我正在像这样使用 ui-sref:

          <div ng-repeat="product in vm.products" class="col-lg-4 col-md-4 col-sm-4 col-xs-4" >
                <a ui-sref="productEdit({id: product.id})">
                    <div style="padding:5px">
                        <div class="card-product">
                            <h4>
                                {{product.name}}
                            </h4>
                            <p>
                                {{product.description}}
                            </p>
                        </div>

                    </div>
                </a>
             </div>

任何人都知道如何让控制器在每次到达页面时重新加载?

【问题讨论】:

    标签: javascript asp.net angularjs angular-ui-router


    【解决方案1】:

    我想你使用 ui-router,根据文档,你可以将可选配置传递给 ui-sref,在你的情况下应该类似于

    <a ui-sref="productEdit({id: product.id})"  ui-sref-opts="{reload: true}">
                        <div style="padding:5px">
                            <div class="card-product">
                                <h4>
                                    {{product.name}}
                                </h4>
                                <p>
                                    {{product.description}}
                                </p>
                            </div>
    
                        </div>
                    </a>
    

    【讨论】:

    • 我试过这个,还有 reload: true 选项。这些都不起作用。
    • 即使接受了反对票也试过这个解决方案适用于 AngularJS V.1.2.2:$state.transitionTo($state.current, $stateParams, { reload: true, inherit: false, notify: true }); ?显然只有控制器才有可能
    • 我试过了,还是不行。像这样: vm.lk = function () { $state.transitionTo('productEdit', { 'id': 1 }, { reload: true, inherit: false, notify: true }); } 并使用 ng-click 调用此函数
    • 尝试添加状态定义缓存:false
    • 我想我解决了这个问题,控制器无法加载依赖项,但由于某种原因没有显示错误。感谢您的帮助!
    猜你喜欢
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 2012-10-20
    • 2015-07-23
    相关资源
    最近更新 更多