1

I have a main controller: MainCtrl and a function within it: $scope.ctrlFn

This function expects an argument and returns a string concatenated to the received argument value.

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);
    }]);

I have a directive: myDirective which creates a template and initiates scope values from parent scope variables and functions and displays them. I have managed to pass the scope variables to directive template like with scope url but not the value myValue which is meant to be assigned the value returned from the parent function.

Directive 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;
          });
        }
    };
});

Here is a plunker: http://plnkr.co/edit/yXE3AuZEPwjlmlqpFTNq?p=preview

2
  • What you want to do? Do you want to access your ctrlFn of controller in your directive? Commented Jun 28, 2017 at 7:17
  • yes @KiranPawar Commented Jun 28, 2017 at 8:07

1 Answer 1

0

To access the parent scope from within the directives template, which is rendered within table cells of the ui grid, you have to go through grid.appScope.variableOrFunction

So to call ctrlFn() I have to:

grid.appScope.ctrlFn()

working example:http://plnkr.co/edit/yXE3AuZEPwjlmlqpFTNq?p=preview

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.