我正在尝试创建动态指令。但我得到错误。有人纠正我吗?
这是我的代码:
var data = [{completed:40}, {completed:20}, {completed:55}, {completed:69}, {completed:71}]
var newApp = angular.module('myApp', []);
newApp
.controller('home', ['$scope', function ($scope, $compile) {
$scope.data = data;
for(var i=0; i < $scope.data.length; i++) {
$('.content').append($compile("<title></title>")(scope));
$scope.$apply();
}
}]);
newApp
.directive('title', function ($compile, $http) {
return {
restrict:'E',
replace : true,
template : '<h2 class="que t3">QQQ</h2>',
link : function () {
console.log('link called');
}
}
});
答案 0 :(得分:0)
遗漏了一些东西:http://jsfiddle.net/8vyduzam/2/
var data = [{completed:40}, {completed:20}, {completed:55}, {completed:69}, {completed:71}]
var newApp = angular.module('myApp', []);
newApp
.controller('home', ['$scope', '$compile', function ($scope, $compile) {
$scope.data = data;
for(var i=0; i < $scope.data.length; i++) {
$('.content').append($compile("<title></title>")($scope));
}
}]);
newApp
.directive('title', function ($compile, $http) {
return {
restrict:'E',
replace : true,
template : '<h2 class="que t3">QQQ</h2>',
link : function () {
console.log('link called');
}
}
});
有三件事
scope
指令而不是title
时,您还使用了$scope
。$digest
循环中,因此您还可以删除额外的$apply()
。