2

I am having trouble binding directive's parameters (using isolated scope) to access them from the directive's custom controller.

Please check this fiddle (open the console, to see the logged results!) http://jsfiddle.net/xj9gqqxn/4/

<div ng-controller="appCtr">
  <div>
    <div custom-directive param="customer.name">
    <span>{{customer.name}}</span>
    <br/>
    <input type="text" ng-model="customer.name"/>
    </div>
  </div>
</div>

When i log inside the directive's link function the value gets logged accordenly, but when i do the same within the constructor function of the controller i see the value as undefined.

If someone know how to access directive's parameters values within the constructor function of the directive's controller, please share the answer or point where i am making a mistake.. sad for me I got no further clues...

Thanks

1 Answer 1

3

Well your controller has same scope as directive so this will work just fine:

app.controller('customController', ['$scope', '$timeout' , function($scope,$timeout) {
    console.log("[customController:new] -  param value is: " + $scope.param);
}])

http://jsfiddle.net/pegla/xj9gqqxn/6/

Also if you are searching for bindToController so directive $scope value is bound to controller instead of $scope, you could do this:

bindToController: {
            param: '='
        },

Updated fiddle: http://jsfiddle.net/pegla/xj9gqqxn/8/

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

3 Comments

Thanks Pegia... that is a clever workaround in order to have the parameter value "at hand" inside the construction function of the directive... i will try this as soon i get to work, and if it works this will be the correct answer!!
I also realize that using bindTOController without an alias (via controllerAs or using the 'controller Identified as alias') is prone to bug

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.