我找不到从 angular-nvd3-directives 函数检索值的方法,我尝试了一切,使用 $emit
、$broadcast
、$scope.varName
。
我在控制台中有正确的值 printend,但无法在模板中访问它。
我希望有人能帮助我,这是代码
在 Controller 中:
$scope.keyName="start"; // this works
$scope.$on("keyName", function(event, mass){
$scope.keyName = mass; //correctly assigned as printed in console
console.log( $scope.keyName);
console.log(event.currentScope); // here I find everything inside the scope
// I'm successuffly using elsewhere in the code
});
$scope.toolTipContentFunction = function(){
return function(key, x, y, e, graph) {
$scope.$broadcast("keyName", key);//this correctly broadcast (or emit) the value
return 'Super New Tooltip' +
'<h1>' + key + '</h1>' +
'<p>' + y + ' at ' + x + '</p>'
}
}
这是 HTML:
<div>
<!-- {{exampleData}} -->
<nvd3-pie-chart
data="exampleData"
id="colorExample"
width="550"
height="350"
x="xFunction()"
y="yFunction()"
toolTips="true"
objectequality="false"
tooltipcontent="toolTipContentFunction()">
{{keyName}}
<svg height="250"></svg>
</nvd3-pie-chart>
</div>
exampleData 已渲染,keyName 未渲染。
任何帮助将不胜感激。
最佳答案
好的,我找到了。我是初学者。
我必须将更改应用到变量,如下所示:
$scope.$apply(function(){
$scope.keyName = mass;
});
所以现在的功能是:
$scope.$on("keyName", function(event, mass){
$scope.$apply(function(){
$scope.keyName = mass;
});
});
这是一篇很有启发性的文章:http://jimhoskins.com/2012/12/17/angularjs-and-apply.html
关于javascript - angularjs-nvd3-directives 函数外部的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24548623/