3

関数 (デリゲート) を AngularJS のディレクティブに渡し、それをリンク関数で使用する方法に少し困惑しています。多分私は完全にオフになっているので、あなたがそれをどのように行うかについての指針は大歓迎です.

私は次のコントローラを持っています

        myApp.controller("QuestionCtrl", function($scope, $http) {

           $scope.filter = function(query) {
            console.log("filter filter", query);
           };

           $scope.replace = function(query, key) {
            console.log('replace replace');
           };
        }

これがマークアップです

<a class="Add Button" href="#" onsearch="filter(query)" onreplace="replace(hello, world)" showpane="#AddQuestionPane"><span>Ny fråga</span></a>

ご覧のとおり、ディレクティブで使用したいスコープ上の関数を指す 2 つの属性があります。

myApp.directive("searchreplace", function() {

        return {
            restrict: 'A',
            scope: {
                onsearch: "&",
                onreplace: "&"
            },
            link: function(scope, element, attrs) {
                element.bind("dblclick", function() {
                    scope.editMode = !scope.editMode;

                    if (scope.editMode) {
                        var replaceBox = angular.element('<input type="text" class="Search" placeholder="Ersätt" />');
                        replaceBox.keyup(function() {
                            //How do I call onsearch? Yes it is the wrong method, just a test
                            scope.onsearch({ query: replaceBox.val() });
                        });
                        element.after(replaceBox);
                    } else {
                        element.siblings(".Search").remove();
                    }
                });
            }
        };
    });

私は一生、スコープでメソッドを呼び出す方法を理解できません。マークアップで指定したい。それは可能ですか?

使用するより良い解決策はありますか?

4

1 に答える 1