你不需要调用父链,你只需要 scope.$parent。因为作用域原型继承自它们的父作用域,所以任何不是值类型的东西都可以安全地访问和修改。查看此答案以了解有关范围继承如何工作的更多信息。
这是您需要的所有代码。我以两种方式实现了通信示例;第一个使用公共模型的依赖注入,第二个引用父范围。
工作的笨蛋
var app = angular.module('myApp', ['ngGrid']);
app.factory('rangeValues', function() {
return [];
});
app.controller('MyCtrl', function($scope, rangeValues) {
$scope.factoryRangeValues = rangeValues;
$scope.parentScopeRangeValues = [];
$scope.myData = [{name: "Moroni", age: 50},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34}];
$scope.gridOptions = {
data: 'myData',
columnDefs: [{field: 'name', displayName: 'Name'},
{field:'age', displayName:'Age', cellTemplate: '<div ng-class="{green: row.getProperty(col.field) > 30}"><div class="ngCellText"><div ng-range value="row.getProperty(col.field)">{{ value }}</div></div></div>'}]
};
});
app.directive('ngRange', function(rangeValues) {
return {
restrict: 'EA',
scope: {
value: '='
},
link: function(scope, element, attrs) {
// Pushing values to the injected model.
rangeValues.push({
value: scope.value
});
// Pushing values to the model on the parent scope
scope.$parent.parentScopeRangeValues.push({
value: scope.value
})
}
}
});