Angular使用指令编译新的dom

时间:2015-07-08 12:14:46

标签: javascript angularjs

我有一个角度应用程序,其中有一个进程我添加了新的DOM元素。 例如:

$("#thingsAppendedHere").append('
        <div id="imNewlyAppended">
            <directive something="someObject" />
        </div>
');

将产生:

<div id="thingsAppendedHere>
    <div id="imNewlyAppended">
        <directive something="someObject" />
    </div>
</div>

和我的指示:

someApp.directive('directive', function() {
        return {
            restrict: 'E',
            templateUrl: 'directive.html',
            controller: DirectiveController,
            scope: {
                something: '='
            }
        }

和汇编是:

var scope = angular.element('#thingsAppendedHere').scope();
var element = angular.element('#imNewlyAppended');

$injector.invoke(function($compile) {
     $compile(element)(scope);
});

问题是手动编译后指令没有$ scope.something。 似乎它的范围是#thingsAppendHere div。 我认为这是因为我在编译时强制了范围。

我能做些什么才能让它发挥作用?如何告诉指令创建自己的隔离范围并绑定&#39; $ scope.something&#39;到外部范围&#39; $ scope.someObject&#39;

亲切的问候。

0 个答案:

没有答案