gpt4 book ai didi

angularjs - AngularJs : do I require $scope in my controller for this?

转载 作者:行者123 更新时间:2023-12-04 10:12:00 25 4
gpt4 key购买 nike

我正在尝试模仿 this Plunker ,特别是在 ag-grid 的每一行中添加一个按钮。

function ageClicked(age) {
window.alert("Age clicked: " + age);
}

function ageCellRendererFunc(params) {
params.$scope.ageClicked = ageClicked;
return '<button ng-click="ageClicked(data.age)">Age</button>';
}

Ag-grid 调用 ageCellRendererFunc渲染单元格。它正在生成一些 HTML 来显示一个按钮,单击该按钮会导致 ageClicked被称为。

params.$scope.ageClicked = ageClicked;似乎正在分配 $scope变量,用于按钮代码: '<button ng-click="ageClicked(data.age)">Age</button>' .

我不确定为什么需要分配 $scope变量,以及为什么我们不能只引用 $scope功能。可以这样做吗?

更重要的是,我不注入(inject) $scope进入我的 Controller ,因为我使用 constroller as View 中的语法。

如何使类似的代码工作,将 HTML 按钮添加到 ag-grid单元格,使用 controller as语法?

[更新] 上面提到的 Plunker 使用了非常旧的 ag-grid 版本。
  • 我想使用最新版本,v22
  • 我不想使用 $scope$rootscope , 只是 thiscontroller as语法
  • 每行应包含一个单元格,该单元格显示一个按钮,单击该按钮时,将执行以 soem 行数据作为参数的功能(就像 Plunker 中的“年龄”,但在此列表中满足 1 和 2)
  • 最佳答案

    这是一个 $scope里面 ag-grid (笨蛋)

    在它调用 ageCellRendererFunc 之前

    function ageCellRendererFunc(params) {
    params.$scope.ageClicked = ageClicked;
    eturn '<button ng-click="ageClicked(data.age)">Age</button>';
    }

    它初始化 params (4012):
    RenderedCell.prototype.createParams = function () {
    var params = {
    node: this.node,
    data: this.node.data,
    value: this.value,
    rowIndex: this.rowIndex,
    colDef: this.column.colDef,
    $scope: this.scope, // <----
    context: this.gridOptionsWrapper.getContext(),
    api: this.gridOptionsWrapper.getApi()
    };
    return params;
    };

    所以你可以使用 controller as
    module.controller("exampleCtrl", function($http) {
    var vm = self;
    /* ... */
    }

    plunger with vm

    编辑 1

    这是 plunker使用 ag-grid 22.0.0

    (已添加 agGrid.initialiseAgGridWithAngular1(angular) )

    这段代码很好:
    function ageCellRendererFunc(params) {
    params.$scope.ageClicked = ageClicked;
    return '<button ng-click="ageClicked(data.age)">Age</button>';
    }

    正如你提到的 $scopeparams 相关而不是你的 Controller 。您的 Controller 不使用 $scope一点也不。这是 ag-grid 的定义。开发人员可以使用另一个变量 - bob语法为 params.bob.ageClicked = ageClicked;

    关于angularjs - AngularJs : do I require $scope in my controller for this?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61294833/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com