简体   繁体   English

AngularJS在指令中追加和编译

[英]AngularJS Append and compile in directive

i have a problem, i wish to append html elements in a directive in angularjs, and i wish to compile them for example i wish to append a {{ text }} and i wish to compile this with ~~~ $scope.text = "example..."; 我有一个问题,我想在angularjs的指令中附加html元素,并希望对其进行编译,例如,我希望附加{{text}},并希望使用~~~ $ scope.text = “例...”;

how can i do this? 我怎样才能做到这一点? is this possible? 这可能吗? how? 怎么样?

(function() {
'use strict';

var matrixelement = document.getElementById("matrixcontent");

var app = angular.module('testviewer', ['ngMaterial', 'ngSanitize']).controller('matrixviewCtrl', matrixviewCtrl);


app.directive("matrixview",function($compile){
    return {
            link: function (scope, iElement, iAttrs) {
            var svg = angular.element("<p>{{ text }}</p>");
            iElement.append($compile(svg)($scope));
        }            
    }
});



function matrixviewCtrl($scope, $compile) {


}
})();

Change this line: 更改此行:

iElement.append($compile(svg)($scope));

To this: 对此:

iElement.append($compile(svg)(scope));

Acutally you only have to remove the $ sign from scope because in your link fuction it is mentioned as scope . 基本上,您只需要从作用域中删除$符号,因为在链接功能中它被称为scope

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM