1

ユーザーが単語をクリックすると、displayPopup() が呼び出され、ここで角度付きアプリを作成します。$scope.$apply 関数でいくつかのデータを撮影する必要があります。そのデータはポップアップに表示されています、 $scope.test() または他の関数を呼び出してアプリを更新すると、TypeError: Object #<Object> has no method 'test'

メソッドを呼び出せないのはなぜですか!?

 displayPopup = function(event) {

    var popup = document.createElement('div');
    popup.innerHTML = popupContent;

    popup.id = "wordly-popup";
    popup.style.top = event.clientY + "px";
    popup.style.left = event.clientX + "px";
    $("body").append(popup);

    var $injector = angular.bootstrap(popup, ['myApp']);
    var $scope = angular.element(popup).scope();

    $scope.$apply(function(){
        $scope.word = getSelectionText();
        $scope.contextSentence = currentSentence.innerHTML;
               $scope.test(); // NOT WORKING


    });

}

そして、ここに私のアプリケーション定義があります:

var myApp = angular.module("myApp", []);

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

    $scope.showLoading = false;
    var currentPOS = null;

    $scope.setPOS = function(pos) {
        currentPOS = pos;
    }

    $scope.getDetails = function() {

        if ($scope.word.length == 0) {
            $scope.definitions = null;
            $scope.partsOfSpeech = {};
        }

        $scope.showLoading = true;

        currentPOS = null;
        $http.get('http://localhost:3000/words/definitions/' + $scope.word).then(function(response) {
            $scope.showLoading = false;
            console.log(response.data[1]);
            $scope.syllables = response.data[1];
            $scope.definitions = response.data[0];
            $scope.partsOfSpeech = _.uniq(_.pluck(response.data[0], "partOfSpeech"));
        });
    };

    $scope.posFilter = function(definition) {
        if (currentPOS == null) return true;
        return definition.partOfSpeech == currentPOS;
    };

    $scope.test = function()
    {
        console.log("hello!");
    }
});

myApp.directive("hovercolor", function() {
    return function(scope, element, attrs) {
        element.bind("mouseenter", function(data) {
            element.css("background-color", "#f89406");
            element.css("color", "white");
            element.css("cursor", "pointer")
        });

        element.bind("mouseleave", function(data) {
            element.css("background-color", "transparent");
            element.css("color", "black");
        });
    };
});
4

1 に答える 1

1

作成してドキュメントに追加したばかりの のスコープを要求しているpopupため、そのスコープを要求すると、取得すべきものを正確に取得できます: $rootScope

というpopupContentHTMLが含まれているとng-controller="PopupCtrl"思いますが、これはおそらくあなたが望むスコープです。ng-controller属性を に設定するか、popupから必要なスコープを取得できますpopup.firstChild

于 2013-05-07T06:06:16.863 に答える