gpt4 book ai didi

javascript - 使用 AngularJS 操作 DOM

转载 作者:行者123 更新时间:2023-11-30 17:19:43 25 4
gpt4 key购买 nike

我正在尝试使用指令通过 angularjs 操作我的表。

我想像这样向第一个 id=2 的 td 添加一个新类:

gameApp.directive('mapActivity', function() {
return {
link: function(scope, element, attrs) {
angular.element('.click#1').addClass('dotted');
}
};
});

我试图在这里“使用”指令:

<map-activity>
<table ng-bind-html="safeHtml()">
</table>
</map-activity>

但是没有任何反应。第一个 TD 没有得到类 'dotted'。我做错了什么?

这是我的 Controller :

var gameApp = angular.module("gameApp", ['ngRoute','ngSanitize']);

gameApp.service('link', function() {
this.user = false;
});
gameApp.filter('unsafe', function($sce) {
return function(val) {
return $sce.trustAsHtml(val);
};
});

gameApp.directive('mapActivity', function() {
return {
priority: 1,
restrict: 'A',
link: function(scope, element, attrs) {
angular.element('.click#1').addClass('dotted');
}
};
});
function makeTableFrom(str) {
var k = 1;
result = "";

for(var i = 1; i <= 8; i++) {
result += '<tr>';

for(var j = 1; j <= 20; j++) {
if(str[k] == '#') {
result += '<td id=' + k + '">#</td>';
}
else if(str[k] == '&') {
result += '<td class="click" val="water" id="' + k + '">&</td>';
}
else {
result += '<td class="click" id="' + k + '"><a href="#"></a></td>';
}

k++;
}
result += '</tr>';
}
return result;
}


gameApp.config(function($routeProvider) {
$routeProvider

.when('/', {
templateUrl : 'partials/firstpage.html',
controller : 'firstPageCtrl'
})

.when('/game', {
templateUrl : 'partials/game.html',
controller : 'gameCtrl'
});

});

gameApp.controller("firstPageCtrl", function($scope,$http,link,$location) {
$scope.doLogin = function() {
$http.post("lib/action.php", {username: $scope.username, password: $scope.password}).success(function(data) {
if(data) {
link.user = data;
console.log(link.user);
$location.path("/game");
}
}).error(function(data) {
console.log(data);
});
};
});


gameApp.controller("gameCtrl", function($scope,$http,link,$location,$sce) {
//$scope.trr = [1,2,3,4,5,6,7,8];
//$scope.tdd = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
$scope.getMonsters = "1";
var map;

$http.post("lib/action.php", {monsters: $scope.getMonsters}).success(function(data) {
map = data;
console.log(map);
$scope.result = makeTableFrom(data);
console.log($scope.result);
});

$scope.safeHtml = function() {
return $sce.trustAsHtml($scope.result);
};
if(link.user) {
/*$scope.message = "fisk";
console.log(link.user);*/
} else {
/*$scope.message = "Ledsen fisk";
console.log("Är inte satt");*/
}

});

如您所见,我使用 javascript 函数为 HTML 分配变量,然后在我的 View 中使用它,将其传递给过滤器。

当我按 Ctrl+u 查看页面源代码时,我看不到正在打印的 td 和 tr。这会影响它为什么不起作用吗?

最佳答案

尝试分配不同的 prioritymap-activity 指令,所以它会在 ng-bind-html

之后处理

正如@Abhishek Jain 和@Dalorzo 所指出的,您的指令必须是应用于同一 DOM 元素的属性

.directive('mapActivity', function() {
return {
priority: 0, // check what priority ng-bind-html have and set it to be more than that.
link: function(scope, element, attrs) {
...
}
}
})

priority

When there are multiple directives defined on a single DOM element, sometimes it is necessary to specify the order in which the directives are applied. The priority is used to sort the directives before their compile functions get called. Priority is defined as a number. Directives with greater numerical priority are compiled first. Pre-link functions are also run in priority order, but post-link functions are run in reverse order. The order of directives with the same priority is undefined. The default priority is 0.

关于javascript - 使用 AngularJS 操作 DOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25364492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com