0

my directive:

angular.module('matrixarMatrice', []).directive('mtxMatriceForm', function () {
  return {
    restrict: 'E',
    templateUrl: 'views/matrice/matrice.html',
    scope: {
      matrix: '=',
      isclicked: '=',
      selectedprice: '&'
    },
    link: function (scope, element, attrs) {
      ...
      scope.selected = function (prices) {
        scope.selected.id = prices.id;
        scope.selectedprice(prices);
      };
    }
  };
});

my controller:

$scope.selectedprice = function (prices) {
  console.log(prices);
};

my html:

<mtx-matrice-form matrix="matrix " isclicked="isclicked" selectedprice="selectedprice(prices)"></mtx-matrice-form>

In my directive when i select item i call my controller. I want to exploit my object prices, but the problem i have at the moment is i have an undefined in my controller. Does anyone know the correct way of doing this?

1
  • Shouldn't this selectedprice="selectedprice(prices)" be selectedprice="selectedprice"? Your code binds the result of calling selectedprice(prices) to the directive's scope.selectedprice. Commented Jan 8, 2015 at 14:01

2 Answers 2

1

This is the one option that you can use to include controller inside a directive! There are other options as well. Hope it helps!

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

app.controller('MainCtrl', function($scope) {
  $scope.name = 'Lorem';
});

app.directive('directives', function() {
  return {
    restrict: 'E',
    controller: function($scope, $element){
      $scope.name = $scope.name + "impsum";
    },
    link: function(scope, el, attr) {
      scope.name = scope.name + "Ipsum";
    }
  }
})
Sign up to request clarification or add additional context in comments.

Comments

0

i have found this solution:

in my directive:

...
scope.selected = function (prices) {
   scope.selected.id = prices.id;
   scope.selectedprice({prices: prices});
};
...

1 Comment

Basically, when you want to pass a function to the directive, you need to pass the parameter as an object. So in you case, it should look like this: <mtx-matrice-form matrix="matrix " isclicked="isclicked" selectedprice="selectedprice({prices: foo})"></mtx-matrice-form>.

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.