我们如何通过模板url动态地为ng模型赋值



我是angular js的新手。我已经编写了一个加载模板url的指令,并尝试更改ng模型值。但是这个值不会改变。

我试着模拟这棵树http://tinyurl.com/hg7ehge只是我试图从url加载html。

以下是plunker代码:http://tinyurl.com/zwmleec

要通过动态名称引用模型,可以隔离作用域(作用域:true)并使用$parent引用外部作用域:

.directive('dynamicmodel', function($templateRequest, $compile) {
    return {
        replace: true,
        restrict: 'E',
        scope: true,
        link: function(scope, element, attrs) {
            scope.name = attrs.comm;
            $templateRequest("template.html").then(function(html) {
                element.replaceWith($compile(html)(scope));
            });
        }
    };
});

然后在模板中使用scope.comm:

<input type="text" ng-model="$parent[name].subject" />

演示:http://plnkr.co/edit/B4HgvxuT1jZJ2H3U9UO9?p=info

最新更新