如何在我的控制器中获取url变量值

时间:2015-05-27 10:40:58

标签: angularjs

我的网址是:http://192.168.1.4:8081/theme11#/createAd

我想删除所有if条件并分配"主题编号变量"直接到$ scope.banner变量。我试过$ routeParams但它没有显示任何值。我是一个更新鲜,并具有angularjs如何工作的基本知识。请帮帮我。

            var myStr = $location.absUrl(); 
            if (myStr.indexOf("theme11") != -1){
                    console.log ("THEME11  ");
                    $scope.banner = "theme11.jpg";
                    }else if(myStr.indexOf("theme2") != -1){
                        console.log ("THEME2");
                        $scope.banner = "theme2.jpg";
                    }else if(myStr.indexOf("theme3") != -1){
                        console.log ("THEME3");
                        $scope.banner = "theme3.jpg";
                    }else if(myStr.indexOf("theme4") != -1){
                        console.log ("THEME4");
                        $scope.banner = "theme4.jpg";
                    }
                    else{
                        $scope.banner = "default.jpg";
                    }   

2 个答案:

答案 0 :(得分:0)

使用拆分

var themeName = $location.path().split("/")[3]
'http://192.168.1.4:8081/theme11#/createAd'.split("/")[3]

themeName包含

themeName = "theme11#"

然后

theme = themeName..split("#")[0]
theme= "theme11"

答案 1 :(得分:0)

也许,试试这个:

var myStr = $location.absUrl(); 
var number = parseInt(myStr.replace((location.protocol+'//'), '').split('/')[1].replace('theme', ''));
$scope.banner = 'theme' + number + '.jpg";

的jsfiddle: https://jsfiddle.net/3Lkencyg/

相关问题