Angular - pass object from directive to parent scope on click

吃可爱长大的小学妹 提交于 2019-12-25 04:07:08

问题


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

    });

}]);

来源:https://stackoverflow.com/questions/41578270/angular-pass-object-from-directive-to-parent-scope-on-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!