如何将$ state.go()中的数据传递给angularProvider中的数据参数?

时间:2016-02-09 17:15:27

标签: angularjs angular-ui-router

我无法使用$ state.go()将一些数据传递给stateprovider。以下是我们一直使用的示例代码。

$stateProvider.state('socialform', {
        url: "/socialform?webcontent",
        templateUrl: "base_template/_Sends.html?",
        data: { pageTitle: 'Social & Website Publishing' },
        resolve: {
            callPreRenderServices: callPreRenderServices
        }
    });


$scope.isWebContent = function(status) {
    if(status) {
        $state.go('socialform', {webcontent:true});
    }
    else {
        $state.go('socialform');
    }        
};

基本上,我们需要做的是将一个title变量传递给$ state.go(),这样它就会将pageTitle替换为传递变量的值。

从上面的代码到这个:

$stateProvider.state('socialform', {
        url: "/socialform?webcontent",
        templateUrl: "base_template/_Sends.html?",
        data: { pageTitle: title },
        resolve: {
            callPreRenderServices: callPreRenderServices
        }
    });


$scope.isWebContent = function(status) {
    if(status) {
        $state.go('socialform', {webcontent:true, title:"some title"});
    }
    else {
        $state.go('socialform', {title:"another title"});
    }        
};

1 个答案:

答案 0 :(得分:0)

您可以使用服务:

function draw() {
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext('2d');
ctx.canvas.width  = window.innerWidth;
ctx.canvas.height = window.innerHeight;
}

然后,您可以通过自定义数据或通过解析函数注入:

module.service('titleService', function() {
    this.title = null;
});

// ... inject titleService in the calling controller ...
$scope.isWebContent = function(status) {
    if(status) {
        titleService.title = 'Some Title'
        $state.go('socialform');
    }
    else {
        titleService.title = 'Another Title'
        $state.go('socialform');
    }        
};

您也可以将其作为$ state参数传递:

// ... inject before route definition, via dependency injection
data = { title: titleService.title };
$stateProvider.state('socialform', {
    url: "/socialform?webcontent",
    templateUrl: "base_template/_Sends.html?",

    // like this
    data: data,

    resolve: {
        callPreRenderServices: callPreRenderServices
        // Or you can resolve your title from your service
        // and use pageTitle in your controller
        pageTitle: ['titleService', function(titleService) { 
            return titleService.title; 
        }]
    }
});