我正在开发一个动态可编辑表格指令,当我刷新页面时,有一半的时间, Angular 分量被解析为其变量的值,并且有一半的时间是在字面上渲染 {{variable}}这一页。这是这个问题的一个小问题:
http://plnkr.co/edit/WvzKYQ?p=preview
angular.module('myApp').directive('hmGrid', function ($window) {
...
link: function (scope, element, attrs) {
...
controller: function ($scope) {
...
});
我有一种感觉,我的链接和 Controller 中的代码(在我的指令中)没有遵循正确的编程实践,因此在页面呈现时没有进行编译。有人可以帮助重新组织代码,以便每次都能成功渲染吗?
最佳答案
尝试将初始化 gridoptions
的代码移至指令的 Controller 中,而不是在单独的 Controller 中初始化 gridoptions,然后将它们传递给指令:
controller: function ($scope, $http) {
$http.get('filename.txt').
success(function(data) {
$scope.data = data.data;
$scope.headers = data.headers;
$scope.headernames = data.headernames;
$scope.gridShowing = true;
$scope.editShowing = false;
$scope.addShowing = false;
$scope.idx = 0;
$scope.reversed = false;
$scope.sortColumn = '';
});
然后您可以从指令中删除 gridoptions
属性,删除不必要的额外 Controller ,这样您的指令就应该每次都能工作。
更新了plunker here .
关于javascript - AngularJS 编译不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33549693/