我有一个主控制器:MainCtrl
及其中的一个功能:$scope.ctrlFn
此函数需要一个参数并返回一个连接到接收参数值的字符串。
MainCtrl JS:
app.controller('MainCtrl', ['$scope', '$http', '$log', '$timeout', function ($scope, $http, $log, $timeout) {
$scope.ctrlFn = function(x) {
return 'some val'+x;
};
var customTemp = '<div ng-repeat="c in grid.appScope.someArray">{{c}}<my-directive url="MODEL_COL_FIELD" vvv="ctrlFn(url)" /></div>';
$scope.someArray=['a','b','c'];
$scope.gridOptions = {};
$http.get('https://cdn.rawgit.com/angular-ui/ui-grid.info/gh-pages/data/500_complex.json')
.success(function(data) {
$scope.gridOptions.data = data;
});
$scope.gridOptions.rowHeight = 50;
$scope.gridOptions.columnDefs = [
{ name:'eee' },
{ name:'age' },
{ name:'address.street'}
];
$scope.gridOptions2 = {};
$scope.gridOptions2.data =[];
$scope.gridOptions2.rowHeight =400;
$scope.gridOptions2.columnDefs =[];
var timer = function() {
for (i = 1; i < 100; i++) {
$scope.gridOptions2.data.push({
value:i,
name:'Name'+i,
url:'https://dummyimage.com/30x30/000/fff&text='+i,
image:i
});
}
$scope.gridOptions2.columnDefs =[
{
name:'image',
cellTemplate:customTemp,
width:100
},
{
name:'name',
cellTemplate:customTemp,
width:100
},
{
name:'url',
width:50
},
{
name:'value'
}
];
};
$timeout(timer,0);
console.log($scope.gridOptions);
console.log($scope.gridOptions2);
}]);
我有一个指令:myDirective
,它创建一个模板并从父作用域变量和函数启动作用域值并显示它们。我已设法将范围变量传递给指令模板,如范围url
,但不是值myValue
,它将被赋予从父函数返回的值。
指令myDirective
:
app.directive('myDirective', function ($log) {
return {
restrict: 'E',
replace: true,
scope: {
url: '=',
vvv: '&'
},
template: '<div ng-init="u=url; myValue=vvv" >{{u}}-{{myValue}}<img src="https://dummyimage.com/100x100/000/fff&text={{url}}" alt="Smiley face" height="100" width="100"></div>',
link: function (scope, element, attrs) {
scope.$watch('url', function(newValue) {
var url = newValue;
});
}
};
});
答案 0 :(得分:0)
要从ui grid
的表格单元格中呈现的指令模板中访问父作用域,您必须通过grid.appScope.variableOrFunction
所以要调用ctrlFn()我必须:
grid.appScope.ctrlFn()