无法使用Angular.js更改标签的href值

时间:2019-06-13 06:54:14

标签: angularjs angular-ui-router href

使用Angular.ja a动态设置ui-router标签值时遇到一个问题。我在下面提供我的代码。

<a ui-sref="{{mastUrl}}" ng-show="isMaster">Master Info</a>
$scope.mastUrl='app.ownerinfo.owner.vew';
var url='../service/admin/login/checkmenu.php';
    var method='GET';
    var data='';
    DataService.connectToServerSideScript(method,url,data)
    .then(function(response) {
        console.log('menu',response);
        if (response.length > 0) {
            angular.forEach(response,function(obj){
                if (obj.user_type==2) {
                    $scope.isUser=false;

                    if (obj.isMast==0) {
                        $scope.isMaster=false;
                    }else{
                        $scope.isMaster=true;
                        if (obj.mastUrl !='') {
                            $scope.mastUrl=obj.mastUrl;
                        }
                    }
                }
            })
        }
    },function(error) {

    })

在这里,我将app.ownerinfo.owner.vew的默认值设置为$scope.mastUrl,但是在服务内部,我需要动态设置它。根据我当前的示例,服务成功响应中的值为app.ownerinfo.owner.new。但是a标记的生成的输出HTML如下所示。

<a ui-sref="app.ownerinfo.owner.new" ng-show="isMaster"
   href="#!/ownerinfo/owner/view">Master Info</a>

我的路由代码如下。

.state('app.ownerinfo',{
        url:'/ownerinfo',
        templateUrl:'view/ownerinfo.html',
        controller:'ownerinfoController'
    })
    .state('app.ownerinfo.owner',{
        url:'/owner',
        templateUrl:'view/owner.html',
        controller:'ownerController'
    })
    .state('app.ownerinfo.owner.vew',{
        url:'/view',
        templateUrl:'view/ownerview.html',
        controller:'ownerviewController'
    })
    .state('app.ownerinfo.owner.new',{
        url:'/new',
        templateUrl:'view/newowner.html',
        controller:'newownerController'
    })

此处ui-serf的值已按预期设置,但href的值与默认值相同。我需要将两个值都设置为相同。

2 个答案:

答案 0 :(得分:0)

我认为更好的方法是将ng-click标记用于控制器方法,并在该方法中使用$ window.location.href ='URL';语法来重定向用户。这样,您的重定向(URL构建)将保留在控制器逻辑中。

答案 1 :(得分:0)

您可以使用ui-state指令,该指令允许动态链接:

<a data-ui-state="selectedState.state" data-ui-state-params="{'myParam':aMyParam}">
       Link to page {{selectedState.name}} with myParam = {{aMyParam}}
  </a>

以下是github关于同一问题的讨论:github

这是对您有帮助的第三个答案:answer

相关问题