硬编码的ui-sref工作,渲染ui-sref不起作用

时间:2014-05-14 08:11:46

标签: angularjs

这有效:

<button type="button" class="btn btn-primary" ui-sref="app.project.edit({projectId: 1})">Back</button>

这不是(点击后没有任何反应):

<button type="button" class="btn btn-primary" ui-sref="{{backstate.state}}">Back</button>

当我在两种情况下查看呈现的网页的源代码时,我得到:

<button class="btn btn-primary" ui-sref="app.project.edit({projectId: 1})" type="button">Back</button>

我做错了什么?

编辑:也许这会有所帮助:

AppControler.js

angular.module("app").controller("AppController", function($scope, $state, $rootScope, SessionService, NavigationService) {
    "use strict";
    $scope.goBack = function () {
        $state.go(NavigationService.getBackstate().state);
     };
});

NavigationService.js

angular.module("app").service("NavigationService", function() {
    "use strict";

    var that = this;

    this.backstate = {
            state : "default state"
    };

    return {
        getBackstate : function() {
            return that.backstate;
        },
        setBackstate : function(backstate) {
            that.backstate.state = backstate.state;
        }
    };
}); 

ScenarioListController.js

angular.module("app").controller("ScenarioListController", function($scope, $modal, $state, ngTableParams, NavigationService, project, scenarios) {
    "use strict";

    // [...]
    NavigationService.setBackstate({ state : "app.project.edit({projectId: " + project.id + "})"});
    // [...]
});

1 个答案:

答案 0 :(得分:0)

这可能是一个有约束力的问题。

如果指令ui-sref在转换为实际值之前绑定到字符串{{backstate.state}},它将尝试解析状态{{backstate.state}}而不是

app.project.edit({projectId: 1})

您可以尝试:

<button type="button" class="btn btn-primary" ng-click="goBack()">Back</button>

function myController($scope, $state) {
   $scope.goBack = function () {
      $state.go($scope.backstate.state);
   }
});