【问题标题】:Pass querystring parameter for component in angularjs在angularjs中为组件传递查询字符串参数
【发布时间】:2017-11-19 02:54:52
【问题描述】:

如何在angular中为组件传递查询字符串参数

app.component('test', {
     templateUrl: '/_directive/test.tpl.html',
     bindings: {
     currenttab: "="
     },
     controllerAs: 'test',
     controller: test  });

我想要这样:

 templateUrl: '/_directive/test.tpl.html?version=' + version;

【问题讨论】:

    标签: angularjs angularjs-directive angular-components


    【解决方案1】:

    你不能使用"/_directive/test.tpl.html" + version作为url,因为它会编译成/_directive/test.tpl.html0.0.1并且这个html没有找到,所以我们应该把它改成"/_directive/test.tpl.html?" + version"/_directive/test.tpl.html?version=" + version以获得更好的结果:

    /_directive/test.tpl.html?0.0.1
    /_directive/test.tpl.html?version=0.0.1
    

    要在所有项目中发送公共参数,我们必须在定义 Angular 应用程序之前设置主题,请参阅示例

    window.version = "0.0.1";
    
    var app = angular.module("app", ["ngRoute"]);
    
    app.component('test', {
        templateUrl: 'partials/home-template.html?' + window.version,
        controller: function() {
            console.log(window.version)
        }
    });
    

    检查 chromefirefox 中的 Network 标签以查看编译模式。

    【讨论】:

    • 你的回答是对的,但是 templateUrl 不能直接获取值。所以我使用 template 字段并使用 ng-transclude & fire on change event on controller
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    相关资源
    最近更新 更多