繁体   English   中英

更新有角d3文本onclick

[英]Update the angular d3 text onclick

要更改复选框的click的textValue不透明度属性。
这是控制器中的代码。

var textvalue = gs.selectAll("text")
                        .data(function (d) { return pie(d); })
                        .enter().append("text")
                        .attr("opacity", function (d, i) {
                            while (d.id != 0) {
                                if (d.phase == "Something") return "1";
                                else { return 0.5; }
                            }
                        })
                        .text(function (d) { return d; })
                        .on("click", function (d) {
                            if (d.id != 0) {
                                window.open("A link");
                            }
                        })

复选框的onclick我称为此方法。

  function callMonitor() {
    alert('in monitor ');
    var appElement = document.querySelector('[ng-app=TechRadarApp]');
    var $scope = angular.element(appElement).scope();
    $scope = $scope.$$childHead;
    $scope.$apply(function () {
        $scope.gs.selectAll("text")
                        .data(function (d) { return pie(d); })
                        .enter().append("text")
                        .attr("opacity", function (d, i) {
                             while (d.id != 0) {
                                if (d.phase == "Something else") return "1";
                                else { return 0.5; }
                            }
                        })
                        .text(function (d) { return d; });
    });

您能帮我弄清楚如何从调用监视器方法更改gs.selectAll(“ text”)。 我是anular和Java脚本的新手,不确定如何访问元素。

这就是我的控制器初始化的方式

 MyApp.controller('MyController', ['$scope', 'MyService', function ($scope, MyService) {
    getData();
    function getData() {
        MyService.getTechData()
            .success(function (returndata) {
            A lot of code

               } ]);

这与控制器初始化的通常方式不同。 这会改变我从外部访问控制器内部元素的方式吗?

这是通常的声明。(从斜角位置复制)

myApp.controller('GreetingController', ['$scope', function($scope) {
  $scope.greeting = 'Hola!';
}]);

该代码的问题是您正在callMonitor函数内创建新的text DOM元素:

    $scope.gs.selectAll("text")
    .data(function (d) { return pie(d); })
    .enter()
    .append("text")//this will create new text DOM elemnet for the data
    .attr("opacity", function (d, i) {
                                 while (d.id != 0) {
                                    if (d.phase == "Something else") return "1";
                                    else { return 0.5; }
                                }
                            })
      .text(function (d) { return d; });

正确的方法是您不创建任何text DOM,因为我假设您在调用此callMonitor函数之前必须已完成此callMonitor

因此,应该仅仅是这样:

$scope.gs.selectAll("text")
        .attr("opacity", function (d, i) {
                     while (d.id != 0) {
                       if (d.phase == "Something else") return "1";
                               else { return 0.5; }
                       }
                     }); 

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM