Angularjs ng在编译的html for指令内重复
我有两个指示:Angularjs ng在编译的html for指令内重复,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我有两个指示: window.app.directive('placeholder', function ($compile, $route, $rootScope) { return { restrict: 'AC', link: function (scope, element, attr) { // Store the placeholder element for later use $rootScope["placeholder_" + at
window.app.directive('placeholder', function ($compile, $route, $rootScope) {
return {
restrict: 'AC',
link: function (scope, element, attr) {
// Store the placeholder element for later use
$rootScope["placeholder_" + attr.placeholder] = element[0];
// Clear the placeholder when navigating
$rootScope.$on('$routeChangeSuccess', function (e, a, b) {
element.html('');
});
}
};
});
window.app.directive('section', function ($compile, $route, $rootScope) {
return {
restrict: 'AC',
link: function (scope, element, attr) {
// Locate the placeholder element
var targetElement = $rootScope["placeholder_" + attr.section];
// Compile the template and bind it to the current scope, and inject it into the placeholder
$(targetElement).html($compile(element.html())(scope));
element.html('');
}
};
});
我用它们基本上把一个部分换成另一个部分的html
如果我有以下html:
<div placeholder="footer"></div>
<div section="footer">
<ul ng-model="items">
<li ng-repeat="item in items"> {{item.Description}}</li>
</ul>
</div>
- {{item.Description}
ng重复似乎不起作用。如果我只是在下面输出{{items}},它会显示良好。此外,我知道绑定正在工作,因为我可以更改项目,它将更新
最后,如果我将ul移到该部分之外,它可以正常工作
所以,我的问题是为什么这不起作用(compileng-ng-repeat-inside指令)
我错过什么了吗
编辑:
让我困惑的是,我能做到:
<div section="footer">
<!-- This Works -->
{{items}}
<!-- This also works -->
<input type="text" ng-model="items[0].Description" />
<!-- This doesn't work -->
<ul ng-model="items">
<li ng-repeat="item in items"> {{item.Description}}</li>
</ul>
</div>
{{items}}
- {{item.Description}
这是行不通的。如果在其作用域中没有精确的副本,它就无法从另一个作用域中评估某个内容。如果您想要两个指令进行通信,请使用require并设置一种方式,以便它们在不处于父子关系的情况下进行通信
有几件事你应该考虑一下。基本上,你所做的就是所谓的转换。Section指令将使用ng transclude捕获客户机定义的代码。使用transclusion,也许您可以在第节的范围内将模板计算为html,然后使用指令通信允许它将html块(已计算)传递给另一个指令。唯一的问题是确保在通过绑定进行更改时发生这种情况。您可能需要在部分中的变量上使用一些$watchs,以便在情况发生变化时通知占位符
您可能需要第三个指令,以便允许节和占位符通过进行通信。在这个例子中,假设我有一个第三个指令,叫做broadcaster。然后节和占位符将需要广播器(即要求:“^broadcaster”),它将为每个指令定义一些接口,以便从节->占位符发送HTML。大概是这样的:
<div broadcaster>
<div placeholder="footer"></div>
<div section="footer">
<ul>...transcluded content</ul>
</div>
</div>
…转包内容
我提供了更多详细信息。在这种情况下,将一个模型绑定到一个输入很好,这让我感到困惑。对于一个模型,你不需要ul上的ng模型。