-->

Angular - pass object from directive to parent sco

2019-09-12 16:49发布

问题:

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

    });

}]);