0

I'm using an angular directive to generate a reusable template and show some data in it. The directive also has an ng-click that should take an object and pass it to the parent controller. I'm kind of stuck, not really sure how to pass that data from the directive controller to the scope of the parent controller. I read here but the circumstances are a bit different.

The js code of the directive:

angular.module("app")
    .directive('userData', function() {
    return {
        restrict: "E",
        templateUrl: "directives/userData/userData.html",
        scope: {
            userObj: "="
        },
        controller: function($scope){

        },
        link: function(scope, elements, attrs, controller){

        }
    }
});

And this is the directive html:

<div class="style" ng-click="displayFullDetails(userObj)">{{userObj.first_name}}</div>

Parent controller:

angular.module("app").controller("parentCtrl", ['$scope', function ($scope) {
    angular.element(document).ready(function () {
        getDataService.getJsonData().then(function (data) {
            $scope.users = data.data;    
        })

    });

}]);
5
  • Can you post your parent controller as well? Commented Jan 10, 2017 at 21:01
  • @JeremyWilken just added the parent controller code. Commented Jan 10, 2017 at 21:03
  • You could add another scope param onClick: '&' to the child and pass in a function from the parent. Then in the child call the parent function with the object. Commented Jan 10, 2017 at 21:21
  • @sledsworth Im not sure I understand. Commented Jan 10, 2017 at 21:50
  • Just make the parent a directive as well. It's more modular, and then you can just use your child directives require option to require the parent and use it's functionality to give the parent the data. Look at the section "Directives that communicate" in these docs: docs.angularjs.org/guide/directive Commented Jan 10, 2017 at 22:40

0

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.