Javascript 在angularjs中的指令创建的postlink中找不到元素
我正在尝试为AngularJS创建一个指令。我的html视图如下所示:Javascript 在angularjs中的指令创建的postlink中找不到元素,javascript,angularjs,raphael,graphael,justgage,Javascript,Angularjs,Raphael,Graphael,Justgage,我正在尝试为AngularJS创建一个指令。我的html视图如下所示: <div id="guageContainer" class="row" ng-controller="guageCtrl"> <gauge ng-repeat="ind in indicators" model="ind"></gauge> </div> 我的指示是: angular.module("pgHome"
<div id="guageContainer" class="row" ng-controller="guageCtrl">
<gauge ng-repeat="ind in indicators" model="ind"></gauge>
</div>
我的指示是:
angular.module("pgHome", [])
.directive("gauge", function ($compile) {
return {
restrict: "E",
scope: { model: "=model" },
compile: function (tElement, tAttrs, transclude) {
tElement.append('<div id="{{model.Name}}"></div>');
return {
post: function (scope, iElement, iAttrs, controller) {
console.log(scope.model.Name);
console.log($("#" + scope.model.Name).length);
var g = new JustGage({
id: scope.model.Name,
value: 348,
min: 120,
max: 400,
title: "test",
label: ""
});
}
};
}
};
});
角度模块(“pgHome”,[])
.指令(“仪表”,函数($compile){
返回{
限制:“E”,
作用域:{model:'=model},
编译:函数(远程通讯、tAttrs、转置){
远程通讯。附加(“”);
返回{
职位:职能部门(范围、部门、iAttrs、控制员){
console.log(scope.model.Name);
log($(“#”+scope.model.Name).length);
var g=新的JustGage({
id:scope.model.Name,
价值:348,
分钟:120,,
最高:400,
标题:“测试”,
标签:“
});
}
};
}
};
});
加载包含此指令的页面时,JustGauge会引发一个错误,指出它找不到具有相应id的元素。请注意,第一个console.log工作正常,但第二个console.log返回0。我也是angularjs的新手,我无法解决这个问题 在$timeout
中包装您的JustGage()调用:
.directive("gauge", function ($compile, $timeout) {
...
$timeout(function() {
var g = new JustGage({...});
});
这使浏览器有机会呈现在compile函数中添加的其他HTML。然后插件可以找到新添加的
div
Angular之前是否包含jQuery?@MarkRajcok是的,如果它与jQuery相关,我认为它一定会抛出错误。顺便说一下,Justgauge不需要jQuery。