Angular ui.router重新加载模板

时间:2016-12-09 12:18:30

标签: javascript jquery html angularjs angular-ui-router

首先,我很抱歉一个很长的问题,但它必须是这样的。不幸的是...

我有几个模板和几个路由逻辑,都链接在不同的文件中。由于某种原因,我想加载的模板(取决于所选的区域),不加载其区域模板。它加载,如果我第二次点击它。请注意我已尝试过这个主题的解决方案:

Reloading current state - refresh data

对我来说没有任何作用。

我设法通过将"{reload:true}"内嵌到html代码中来解决其中一个(另一个区域),如下所示:

<a data-ui-sref="uk-web" data-ui-sref-opts="{reload:true}">Energy Consultant</a>

现在我为另一个模板做了同样的事情,但它无法正常工作。这是我的HTML:

<div class="btn us-btn" data-ng-controller="carCtrlUs">
            <script type="text/ng-template" id="careersTplUs.html">
                <div class="closer">
                    <span class="close-me" data-ng-click="ok()">X</span>
                </div>
                <uib-accordion close-others="true">
                    <div uib-accordion-group class="modal-body modone panel-default pull-right" is-open="false">
                        <uib-accordion-heading>
                            <p class="car-heading">Sales Department <i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-down': status.open, 'glyphicon-chevron-right': !status.open}"></i>
                            </p>
                        </uib-accordion-heading>
                        <ul>
                            <li><a data-ui-sref="us-web" data-ui-sref-opts="{reload:true}">Energy Consultant</a></li>
                            <li><a data-ui-sref="us-crm" data-ui-sref-opts="{reload:true}">Client Relationship Manager</a></li>
                        </ul>
                    </div>
                    <div class="modal-body modtwo panel-default pull-right" uib-accordion-group is-open="false">
                        <uib-accordion-heading>
                            <p class="car-heading">Marketing Department <i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-down': status.open, 'glyphicon-chevron-right': !status.open}"></i>
                            </p>
                        </uib-accordion-heading>
                        <ul>
                            <li><a data-ui-sref="us-web" data-ui-sref-opts="{reload:true}">Web Developer</a></li>
                            <li><a data-ui-sref="us-crm" data-ui-sref-opts="{reload:true}">Marketing Coordinator</a></li>
                        </ul>
                </div>
        </uib-accordion>
    <div class="show-me" ui-view></div>
     </script> 
     <button class="btn" ng-click="open()" data-ui-sref="us-intro">United States</button>
</div>

app.js:

var app = angular.module('carApp', ['ngAnimate', 'ngSanitize', 'ui.bootstrap', 'ui.router']);

控制器,用于此模板:

app.controller('carCtrlUs', function($scope, $http, $uibModal) {
    $http.get('json/jobs-us.json').then(function(response) {
        $scope.placeholder = response.data.default;
        $scope.specs = response.data.specs;

        $scope.open = function() {

            var modalContent = $uibModal.open({
                templateUrl: 'careersTplUs.html',
                controller : 'modalContentCtrlUs',
                controllerAs: '$ctrl',
                size: 'lg',
                backdropClass: 'backdropOver',
                openedClass: 'modal-opened',
                resolve: { 
                    items: function() { return $scope.specs; },
                    items2: function() { return $scope.placeholder;}
                }
            })
        console.log($scope.placeholder);
        console.log($scope.specs);
        console.log($scope.specs.web-dev);
        }
    });
});

app.controller('modalContentCtrlUs', function($scope, $uibModalInstance, items, items2) {
    $scope.specs = items;
    $scope.placeholder = items2;
    $scope.ok = function() {
        $uibModalInstance.close();
    }
});

factory.js:

app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state("us-intro", {
            url:"/intro",
            templateUrl: "templates/us/start.html"
        })
    $stateProvider
        .state("us-web", {
            url: "/web-developer/",
            templateUrl: "templates/us/web-developer.html",
        })
        .state("us-crm", {
            url: "/crm/",
            templateUrl: "templates/us/crm.html"
        })
        /*next .state*/
}]);

模板:

<div ng-repeat="(k, v) in specs.webdev">
    <h3>{{::v["job-title"]}}</h3>
    <p>{{::v["job-body"]}}</p>
    United States Apply Here:
    <p>{{::v["job-apply"]}}</p>
</div>

正如你所看到我正在使用ui-bootstrap(模态和手风琴,因为它都在模态窗口中)。

我可以做些什么来让它立即刷新?我已经坚持了好几天,这令人沮丧......我在这里做错了什么?

编辑:请注意,我知道“::”用于单向数据绑定。我试过没有它,结果是一样的。我可以通过将有问题的数据放在.json文件中来解决这个问题,但这确实是一个丑陋的黑客攻击。

3 个答案:

答案 0 :(得分:1)

<div ng-repeat="(k, v) in specs.webdev">
    <h3>{{::v["job-title"]}}</h3>
    <p>{{::v["job-body"]}}</p>
    United States Apply Here:
    <p>{{::v["job-apply"]}}</p>
</div>

The "::" avoid data binding to reduce digest cycle. Try to remove them.

答案 1 :(得分:0)

<a data-ui-sref="uk-web" data-ui-sref-opts="{reload:true}">Energy Consultant</a>

// it should be

<a data-ui-sref="us-web" data-ui-sref-opts="{reload:true}">Energy Consultant</a>

答案 2 :(得分:0)

I have solved this..What I wanted to do is to make the "URL" the same. I have changed the url to manifest the region by the region code, so it is working now.

For example:

UK:

$stateProvider
        .state("de-web", {
            url: "/web-developer-de",
            templateUrl: "templates/de/web-developer.html",
        })
        .state("de-crm", {
            url: "/crm-de",
            templateUrl: "templates/de/crm.html"
        })

Italy:

$stateProvider
        .state("it-web", {
            url: "/web-developer-it",
            templateUrl: "templates/it/web-developer.html",
        })
        .state("it-crm", {
            url: "/crm-it",
            templateUrl: "templates/it/crm.html"
        })

United States:

$stateProvider
        .state("us-web", {
            url: "/web-developer-us",
            templateUrl: "templates/de/web-developer.html",
        })
        .state("us-crm", {
            url: "/crm-us",
            templateUrl: "templates/de/crm.html"
        })

Although this works, I'm not happy. How can it be achieved that the templates are being loaded into the same url?