首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AngularJS编译模板并在Showdown扩展中使用它

AngularJS编译模板并在Showdown扩展中使用它
EN

Stack Overflow用户
提问于 2015-02-20 07:30:23
回答 1查看 433关注 0票数 1

我试图在我的角度应用程序中创建一个Showdown扩展,它将显示范围变量。我能够很容易地设置它来显示基本的作用域变量,但是现在我想把它带到可以使用ng-repeat的结果的地方,并且除了[[object HTMLUListElement]]之外,我无法得到任何显示。

到目前为止,这是我的控制器:

代码语言:javascript
复制
app.controller('MyCtrl', ['$scope', '$window', '$compile', function($scope, $window, $compile){
    $scope.machines = [
        { abbv: 'DNS', name: 'Did Not Supply' },
        { abbv: 'TDK', name: 'The Dark Knight' },
        { abbv: 'NGG', name: 'No Good Gofers'}
    ];
    $scope.machine = $scope.machines[0];

    $scope.machine_list = $compile('<ul><li ng-repeat="m in machines">{{m.abbv}}: {{m.name}}</li></ul>')($scope);
  $scope.md = "{{ machine_list }}";

    var scopevars = function(converter) {
        return [
            { type: 'lang', regex: '{{(.+?)}}', replace: function(match, scope_var){
                scope_var = scope_var.trim();

                return $scope.$eval(scope_var);
            }}
        ];
    };
    // Client-side export
    $window.Showdown.extensions.scopevars = scopevars;
}]);

Plunkr:目前为止的代码

我觉得我必须接近,但现在我不知道我是在完全错误的轨道上,或这是一个摊牌,或一个角的东西或什么。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-21 05:46:54

我意识到我这样做是在角质地战斗(而且严重地失去了安静)。控制器中的DOM是不-不。现在,我有点生气,因为一旦我开始正确地思考和看指令,它是多么的容易。

现在,我没有尝试在控制器中执行编译和所有操作,而是在我使用的指令中包含了$compile服务,因此:

代码语言:javascript
复制
htmlText = converter.makeHtml(val);
element.html(htmlText);

变成:

代码语言:javascript
复制
htmlText = converter.makeHtml(val);
element.html(htmlText);
$compile(element.contents())(scope);

有了这些修改之后,我不再需要扩展中只进行基本计算的部分,因为它正在编译,{{ machine.name }}是自动转换的。

但这仍然使我无法指定一个变量来插入一个模板,仅仅是变量。但是现在输出要通过角编译,我可以将模板放到一个部分中,并使用一个扩展将模式转换为一个工作的ng-include

新的主计长守则:

代码语言:javascript
复制
app.controller('MyCtrl', ['$scope', '$window', '$compile', function($scope, $window, $compile){
    $scope.machines = [
        { abbv: 'DNS', name: 'Did Not Supply' },
        { abbv: 'TDK', name: 'The Dark Knight' },
        { abbv: 'NGG', name: 'No Good Gofers'},
        { abbv: 'TotAN', name:'Tales of the Arabian Nights'}
    ];
    $scope.machine = $scope.machines[0];

  $scope.tpls = {
    'machinelist': 'partials/ml.html'
  };
  $scope.md = "{{machines.length}}\n\n{{include machinelist}}";

    var scopevars = function(converter) {
        return [
          { type: 'lang', regex: '{{include(.+?)}}', replace: function(match, val){
            val = val.trim();
            if($scope.tpls[val] !== undefined){
              return '<ng-include src="\''+$scope.tpls[val]+'\'"></ng-include>';
            } else {
              return '<pre class="no-tpl">no tpl named '+val+'</pre>';
            }
          }}
        ];
    };
    // Client-side export
    $window.Showdown.extensions.scopevars = scopevars;
}]);

当然还有新的柱塞

希望这能对以后的人有所帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28623714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档