gpt4 book ai didi

javascript - 如何使用 $scope 变量在 celltemplate 中的 ui-grid 中使用 ng-show

转载 作者:行者123 更新时间:2023-11-29 21:45:45 25 4
gpt4 key购买 nike

我正在尝试显示基于范围变量的元素。我似乎无法让它工作。由于 $scope.hi 存在,我希望显示“Active”一词。这是我的代码:

var app = angular.module('app', ['ngTouch', 'ui.grid']);

app.controller('MainCtrl', function ($scope, $timeout) {
$scope.hi = true;
$scope.gridOpts = {
columnDefs: [
{ name:'name', field: 'name' },
{ name:'isActive', field: 'isActive'},
{ name:'template',cellTemplate:'<div><a ng-show="hi">Active</a><a ng-hide={{row.entity.isActive=="N"}}>Deactive</a></div>'}
]
};

$scope.waiting = "Waiting...";
$timeout(function () {
$scope.gridOpts.data = [{ name: 'Bob' ,isActive:'Y'}];
}, 3000)
.then(function() {
$timeout( function() {
$scope.gridOpts.data = [{ name: 'John',isActive:'N' }];
}, 3000);
$scope.waiting = "Waiting again...";
})
});

http://plnkr.co/edit/eLFjp8qJCtBRLTrQ7GE5?p=preview

最佳答案

ui-grid 使用隔离范围创建其网格,这意味着该模板背后的范围不是您在其中定义模板的范围。

然而,该独立作用域包含对该原始作用域的引用,您可以将其作为 grid.appScope 进行访问。

如果将 hi 更改为 grid.appScope.hi,则您的示例有效。 Forked version here.

参见 ui-grid API docs on appScope :

appScope: reference to the application scope (the parent scope of the ui-grid element). Assigned in ui-grid controller

use gridOptions.appScopeProvider to override the default assignment of $scope.$parent with any reference

关于javascript - 如何使用 $scope 变量在 celltemplate 中的 ui-grid 中使用 ng-show,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31277673/

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