首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用F5刷新( angularJS ) ngview

使用F5刷新( angularJS ) ngview
EN

Stack Overflow用户
提问于 2013-11-20 19:08:31
回答 3查看 7.5K关注 0票数 5

我的角度应用程序有问题。应用程序第一次运行良好,可以在内部导航,但是当我尝试用F5刷新页面时,它失败了。当按下F5键时,将部分调用到服务器,服务器显然使用部分视图进行响应,而不是整个应用程序。

节点路由:

代码语言:javascript
复制
app.get('/', node_routes.welcome);
...
app.get('/profile', pass.ensureAuthenticated, profile_routes.profile);
app.get('/profile/:name', pass.ensureAuthenticated, profile_routes.partials);
app.post('/profile/update', pass.ensureAuthenticated, profile_routes.update);
...

主计长:

代码语言:javascript
复制
exports.profile = function(req, res) {
    var user = req.user;
    Profile.findOne({ username: user.username }, function(err, profile) {
        if (err) { 
            res.redirect('/'); 
        }       
        res.render("profile");
    });  
};  

exports.partials = function(req, res) {
    var name = req.params.name;
    var user = req.user;
    Profile.findOne({ username: user.username }, function(err, profile) {
        if (err) { 
            res.redirect('/'); 
        }       
        res.render(path.join(__dirname + '/../views/profile/' + name));
    });
};

exports.update = function(req, res){
    var profile = req.body;
    delete profile._id;
    Profile.update({'username':profile.username},profile,{safe:true}, function(err, result){
        if(err) {
            console.log('Error updating profile. ' + err);
            res.redirect('/profile');
        }
        else{
            console.log('' + result + ' profile updated for user: ' + profile.username);
            res.redirect('/profile');
        }           
    });
};

角度应用

代码语言:javascript
复制
myApp.config(['$routeProvider','$locationProvider', function($routeProvider,$locationProvider){
    $routeProvider
       .when('/profile/update', {
           controller: 'myJobOfferListCtrl',
           templateUrl: '/profile/update',
           reloadOnSearch: false
       })
       .when('/profile', {
           controller: 'myJobOfferListCtrl',
           templateUrl: '/profile/dashboard'
       })
       .when('/profile/dashboard', {
           controller: 'myJobOfferListCtrl',
           templateUrl: '/profile/dashboard',
           reloadOnSearch: false
       })
       .when('/profile/offers', {
           controller: 'myJobOfferListCtrl',
           templateUrl: '/profile/offers',
           reloadOnSearch: false
       })      
       .otherwise({redirectTo: '/profile'});
    $locationProvider.html5Mode(true);
}]);

我的个人资料页

代码语言:javascript
复制
extends layout

block content   
   div.container-fluid(ng-app="appProfile", ng-controller="myJobOfferListCtrl", ng-init="initProfile()")
      div.row-fluid
         div.span3(ng-controller="navBarCtrl")
           div.well.sidebar-nav
             ul.nav.nav-list
               li.well.nav-header.
                  My Profile ({{data.profile.username}})
               li(ng-class="{ active: isActive('dashboard')}") 
                  a(href="/profile/dashboard") Dashboard   
               li(ng-class="{ active: isActive('update')}") 
                  a(href="/profile/update") Update profile
               li.divider
               li.well.nav-header My Jobs
               li(ng-class="{ active: isActive('offers')}") 
                  a(href="/profile/offers") Offers
               li(ng-class="{ active: isActive('application')}") 
                  a(href="/profile/application") Applications
         div.span9(ng-view, ng-cloak)

和一个示例部分视图页。

代码语言:javascript
复制
   div.row-fluid 
      div(ng-init="initMyOffers()")  
         accordion.span8.offset1(close-others="true")
            accordion-group(ng-repeat="item in data.myJobs", heading="{{item.title}}")
               accordion-heading
                  {{item.title}}
                  i.pull-right.icon-remove-circle.removeButton(ng:click="remove(item)") 
               p {{item.description}}
               hr
               div.footer
                  div.pull-left 
                     i.icon-calendar.calendar
                     {{item.dueDate | date:'d MMMM yyyy'}}
                  div.pull-right 
                     i.icon-user 
                     {{item.author}}

使用F5刷新时,如何重新加载部分页面??我所期望的角度是,当尝试刷新页面/profile/dashboard时,部分/views/profile/dashboard.jade被调用,但是views/profile.jade也被调用。那么$scope呢?

抱歉,但我有点吐露.谢谢你的帮助!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-01-21 15:25:01

最后,实现了artur提出的解决方案:

代码语言:javascript
复制
<script>
  if (typeof angular == 'undefined')
     window.location.replace("/main_url");
</script>
票数 1
EN

Stack Overflow用户

发布于 2013-11-20 21:54:14

我认为这是一个常见的问题,用户是,而不是,应该在应用程序中使用F5。我不认为,在按下F5之后,可以停止默认浏览器操作。

一个简单的解决方案是将这个或类似的script添加到每个视图中:

代码语言:javascript
复制
<script>
    if (angular == undefined)
        // alert("It is SPA (Single Page Application) -- do not press F5 anymore, please.");
        window.location.replace("your/main/url");
</script>

更复杂的是实现以下场景。

  1. 用户点击f5
  2. 服务器发送当前url current_url的部分视图
  3. 客户端代码检测缺少angular并发送重定向到current_url的请求
  4. 服务器发送修改版本的index.html文件,例如一些隐藏的输入字段,以存储current_url
  5. 加载角后,应用程序将检查隐藏字段并相应地更改位置。
票数 4
EN

Stack Overflow用户

发布于 2015-09-24 12:03:46

当刷新一个有角度的应用程序时,避免丢失东西的最简单和最优雅的方法是使用$cookies服务在cookie中存储所需的任何值。下面是一个例子。希望这能有所帮助。

代码语言:javascript
复制
"use strict";
angular.module('myApp').factory('SessionSrv', [ '$cookies', function($cookies){

    var _myString = $cookies.get('myString'); // e.g. "foo"
    var _myObject = $cookies.getObject('myObject'); // e.g. {foo: "bar"}

    return {

        setMyString: function(myString){
            _myString = myString;
            $cookies.put('myString', _myString);
        },

        getMyString: function(){
            return _myString;
        },

        setMyObject: function(myObject){
            _myObject = myObject;
            $cookies.putObject('myObject', _myObject);
        },

        getMyObject: function(){
            return _myObject;
        },

    };
}]);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20104772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档