Calling directive function from controller angularjs

HUSTLIN

I have this controller

.controller('MyCtrl', ['$scope', '$rootScope', 'restService',
    function ($scope, $rootScope, restService) {                    
        $scope.saveTask = function (workflow, form) {
            if (form.$valid) {
                if (workflow.isSaved == false) {
                    angular.toJson(workflow);
                    $rootScope.$broadcast('saveOrDelete', 1); /*HERE IS CALLING*/
                };
            }
        };
    }
]);           

And my directive with saveOrDelete function:

.directive('logWork', [function() {
    return {
    restrict: 'AE',
    link: function(scope, element, attrs) {
            scope.saveOrDelete = function(option) {
                switch (option) {
                    case 1: {
                        angular.element(element).find('.note-input').css('width', '85%');
                        angular.element(element).find('.active-accept-button').css('display', 'none');
                        angular.element(element).find('.active-delete-button').css('display', 'inline-block');
                    } break;

                    case 2: {
                        angular.element(element).find('.note-input').css('width', '70%');
                        angular.element(element).find('.active-accept-button').css('display', 'inline-block');
                        angular.element(element).find('.active-delete-button').css('display', 'inline-block');
                    } break;
                };
            };
        },
                templateUrl: Global.directives.worklogblock
    };
}])

And I have a problem, I can't call my saveOrDelete function from controller. How I can do this? Please help me to solve this issue.

ste2425

In your directive you need to listen to the saveOrDelete event your emitting in your controller. You then have access to the arguments passed into the broadcast or emit and can work with them.

$scope.$on('saveOrDelete', function(event, option) { $scope.saveOrDelete(option); });

Be aware anything in your application will be fired if they are listening to this event.

Not too sure if your aware of the difference between broadcast and emit, there listed in the docs below. They can give strange behavior if your not aware, thought i best mention it also.

EDIT: On a side note your wrapping your element in angular.element i don't think this is entirely necessary. If you have JQuery included before angular then the element object will be a full JQuery object. If not it will be a JQueryLite object.

Emit and broadcast docs, you will also find the docs on listening to events ($on) on this page: https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$emit

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

AngularJS: Exeucting function from controller in a directive

分類Dev

How to pass a function handler from controller into directive isolated scope in AngularJs?

分類Dev

Angular directive calling parent controller function

分類Dev

AngularJS Passing changing values from Controller to a Directive Controller

分類Dev

Controller/Directive inheritance in AngularJS

分類Dev

Call function from directive in $rootScope - AngularJS

分類Dev

Phalcon - Calling function from other controller

分類Dev

Calling parent View Controller function from a subview

分類Dev

AngularJS: Initialize a controller for each directive

分類Dev

Calling Global Variable from Controller to HTML View AngularJS

分類Dev

Calling a parent directive's function in a child directive

分類Dev

Call controller function from service in angularjs

分類Dev

Call link function from controller Angularjs

分類Dev

How to call controller function from other controller angularjs

分類Dev

Typescript angular directive controller function

分類Dev

Calling function from the same controller to return the view with flash message

分類Dev

Updates controller-value and call controller-function from a directive. controller-calue not updated before function is called

分類Dev

Calling function conditionally in AngularJS

分類Dev

Pass a value from a controller to directive

分類Dev

Calling an AngularJS function from outside the scope of an Angular App

分類Dev

AngularJS pass parameters in directive template to controller

分類Dev

How can controller talk to a directive in AngularJS?

分類Dev

AngularJS: ngChange not called from directive

分類Dev

Angular Directive bind function with & not passing arguments to controller

分類Dev

Problems with passing callback from controller to directive

分類Dev

angularjs directive watch not fired on object function property

分類Dev

AngularJS directive scope function not being called

分類Dev

Calling a function from within a function

分類Dev

Refer to HTML element from directive angularjs

Related 関連記事

  1. 1

    AngularJS: Exeucting function from controller in a directive

  2. 2

    How to pass a function handler from controller into directive isolated scope in AngularJs?

  3. 3

    Angular directive calling parent controller function

  4. 4

    AngularJS Passing changing values from Controller to a Directive Controller

  5. 5

    Controller/Directive inheritance in AngularJS

  6. 6

    Call function from directive in $rootScope - AngularJS

  7. 7

    Phalcon - Calling function from other controller

  8. 8

    Calling parent View Controller function from a subview

  9. 9

    AngularJS: Initialize a controller for each directive

  10. 10

    Calling Global Variable from Controller to HTML View AngularJS

  11. 11

    Calling a parent directive's function in a child directive

  12. 12

    Call controller function from service in angularjs

  13. 13

    Call link function from controller Angularjs

  14. 14

    How to call controller function from other controller angularjs

  15. 15

    Typescript angular directive controller function

  16. 16

    Calling function from the same controller to return the view with flash message

  17. 17

    Updates controller-value and call controller-function from a directive. controller-calue not updated before function is called

  18. 18

    Calling function conditionally in AngularJS

  19. 19

    Pass a value from a controller to directive

  20. 20

    Calling an AngularJS function from outside the scope of an Angular App

  21. 21

    AngularJS pass parameters in directive template to controller

  22. 22

    How can controller talk to a directive in AngularJS?

  23. 23

    AngularJS: ngChange not called from directive

  24. 24

    Angular Directive bind function with & not passing arguments to controller

  25. 25

    Problems with passing callback from controller to directive

  26. 26

    angularjs directive watch not fired on object function property

  27. 27

    AngularJS directive scope function not being called

  28. 28

    Calling a function from within a function

  29. 29

    Refer to HTML element from directive angularjs

ホットタグ

アーカイブ