javascript - angularjs-nvd3-directives 函数外部的值

标签 javascript angularjs angularjs-directive angularjs-scope nvd3.js

我找不到从 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/

相关文章:

javascript - 触发点击有时不起作用

javascript - 无法安装 npm,在执行 FCC node.js 挑战时出现 module.js 错误

javascript - 如何配置 Firefox 以允许 Javascript 拦截从剪贴板粘贴的值?

javascript - 图像的 Ng-repeat,但第一张图像位于不同的 div 类中

css - 如何跟踪 Angular 中的全局鼠标位置?

javascript - 指令内的随机 ng-show

javascript - 在 AngularJS 中创建子范围的所有指令是什么?

javascript - 增加对象数量而不是在数组中创建新对象

javascript - 使用 javascript/jquery 上传带有删除选项的多个文件

html - 如何在 Angularjs 中计算总和和逗号值?