【发布时间】:2023-03-29 16:15:01
【问题描述】:
我发现当我使用 ng-include 时,它被调用的频率太高了。
每次单击Nothing 按钮之一或更改视图或在输入框中键入内容时,getView 都会运行多次。更改视图时最多 6 次。基本上做任何改变$scope 的事情都会产生对getView 的调用。
我创建了一个 plunker 来显示我所描述的行为:plnkr.co
这是正常行为吗?有没有办法让它只运行一次?我担心我可能会因此而失去性能。
我的密码:
index.html
<body ng-app="includeExample">
<div ng-controller="ExampleController">
<div class="row">
<input type="text" ng-model="unrelated">
</div>
<div class="row">
<tabset>
<tab heading="View 1">
<ng-include src="getView('template1')"></ng-include>
</tab>
<tab heading="View 2">
<ng-include src="getView('template2')"></ng-include>
</tab>
</tabset>
</div>
</div>
</body>
Script.js
angular.module('includeExample', ['ngAnimate', 'ui.bootstrap'])
.controller('ExampleController', ['$scope',
function($scope) {
$scope.getView = function(filename) {
console.log("getView " + new Date());
return filename + ".html";
};
}
]);
模板1.html
Content of template1.html
<button type="button" class="btn btn-primary" ng-click="">Nothing</button>
【问题讨论】:
标签: javascript angularjs angular-ui-bootstrap