javascript - Angularjs 页面跳转后报错
PHP中文网
PHP中文网 2017-04-11 12:53:42
[JavaScript讨论组]

报错内容:

TypeError: Cannot read property 'replace' of undefined
TypeError: Cannot read property 'indexOf' of undefined

跳转前页面代码:

跳转前页面controller相关代码:

$scope.linkToDetail=function(msgId) {
        $state.go('detail',{'msgId':msgId});    
    };

详情页html:


    
    

{{msgdata.user.name}}

{{msgdata.created_at|formattime}}

{{msgdata.source|formatsource}}

跳转后的详情页controller:

.controller('detailCtrl', ['$scope','roufanservice','$state','$stateParams',
    function($scope,roufanservice,$state,$stateParams){
        var msgid=$stateParams.msgId;
        var sendData={
                token:localStorage.getItem('authorize.token'),
                secret:localStorage.getItem('authorize.secret'),
                path:'statuses/show.json?format=html&id='+msgid,
                param:'{}'
            };
        var promise=roufanservice.getData(sendData,'get');
        promise.then(function(data){
            $scope.msgdata=JSON.parse(data.result);
        },
        function(err){
            roufanservice.showErrMsg(err);
        });    
}]);

详情页的url:

http://localhost:8100/#/detail?msgId=qIsnp6YWOQs

报的错中的replace来自两个filter,其他页面不报错,只要到了详情页就开始报错

.filter('linkformat',function() {
    return function(content){
        var userlink='';
        return content.replace(//g,userlink);
    };
})
.filter('formatsource',function(){
    return function(source){
        if(source.indexOf('href')>0){
            return angular.element(source).contents()[0].data;
        }
        else{
            return source;
        }
    };
})

网上搜了下,可能跟$locationProvider.html5Mode有关,但是看的一头雾水,请大家帮忙解答一下,谢谢!

PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(2)
怪我咯

这个问题最方便的解决方法是在过滤器中加一层判断

.filter('linkformat',function() {
    return function(content){
    if(content){
    var userlink='<a href="#/tab/detail" class="former">';
        return content.replace(/<a[\s]+href=['"]([^"]*)['"[\s]+class="former">/g,userlink);
    }
    return '';
        
    };
})
高洛峰

不明白为什么页面跳转走了还会报这个错?

你的这个

ng-bind-html="item.text | linkformat" 

不是应该在跳转前就渲染过了嘛?

跳转走了为什么还会报filter的错?难道你页面跳转以后又使用了这俩filter? 不太懂~

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号