AngularJs-控制器和指令之间的通信

时间:2018-12-13 20:07:34

标签: angularjs function controller call directive

例如;

GetSwatchCartListFunc:如何直接从指令访问此方法

homeController.js

(function (App) {
    var Home = function Home($scope, MemberService) {


        $scope.GetSwatchCartListFunc = function (patternDetailLineId) {
            MemberService.GetSwatchCartList(patternDetailLineId).then(function (res) {
                console.log(res.data);
                if (res.data.status === ServiceResulState.Success) {
                    $scope.SwatchCartListData = res.data.resultList;
                    $('#swathcartList').modal('show');
                } else if (res.data.status === ServiceResulState.Fail) {
                    toastr.warning(res.data.message);
                    $('#userLoginModal').modal('show');
                }
            });
        };


    };

    Home.$inject = ["$scope", "MemberService"];

    App.controller("HomeController", Home);
})(App);

directive.js

(function (App) {
App.directive("ngPatternPre", ['toastr', function (toastr) {
    return {
        restrict: 'E',
        templateUrl: '/views/Directives/NgPattern.html'    
        }
    };
}]);})(App);

NgPattern.html

<a ng-click="SwatchCartList(pId)" title="Add"><i class="ti-Line-Gallery"></i></a>

1 个答案:

答案 0 :(得分:1)

要解决 directive.js

中的问题,需要进行以下更改

directive.js

(function (App) {
App.directive("ngPatternPre", ['toastr', function (toastr) {
    return {
        restrict: 'E',
        templateUrl: '/views/Directives/NgPattern.html',
        link: function (scope, element, attrs) {                

            scope.SwatchCartList = function (pId) {
                scope.GetSwatchCartListFunc(pId);                   
            };

        }
    };
}]);})(App);