Undefined不是AngularJS指令中的函数吗?

时间:2014-01-01 19:32:58

标签: angularjs

我有以下指令:

app.directive("markdown", function ($compile, $http) {
    var converter = new Markdown.converter();
    return {
        restrict: 'E',
        replace: true,
        link: function (scope, element, attrs) {
                element.html(converter.makeHtml(element.text()));
        }
    };
});

我这样称呼指令:

<markdown ng-model="q.qv.text"></markdown>

当我运行时,我收到消息:

TypeError: undefined is not a function
    at http://127.0.0.1:81/Content/app/common/directives/xx.js:86:21
    at Object.d [as invoke] (http://127.0.0.1:81/Scripts/angular-v1.2.2/angular.min.js:30:256)
    at http://127.0.0.1:81/Scripts/angular-v1.2.2/angular.min.js:40:186
    at Array.forEach (native)
    at q (http://127.0.0.1:81/Scripts/angular-v1.2.2/angular.min.js:7:255)
    at Object.<anonymous> (http://127.0.0.1:81/Scripts/angular-v1.2.2/angular.min.js:40:153)
    at Object.d [as invoke] (http://127.0.0.1:81/Scripts/angular-v1.2.2/angular.min.js:31:179)
    at http://127.0.0.1:81/Scripts/angular-v1.2.2/angular.min.js:32:441
    at Object.c [as get] (http://127.0.0.1:81/Scripts/angular-v1.2.2/angular.min.js:29:478)
    at ia (http://127.0.0.1:81/Scripts/angular-v1.2.2/angular.min.js:53:213) 

消息指向该行:“var converter = new Markdown.converter();”

请注意,我已经有另一个看起来像这样的指令:

app.directive('uiPagedownBootstrap', function ($compile) {
    var nextId = 0;

    //Make converter only once to save a bit of load each time - thanks to ajoslin
    var markdownConverter = new Markdown.Converter();

    return {
        require: 'ngModel',
        replace: true,
        template: '<div class="pagedown-bootstrap-editor"></div>',
        link: function (scope, iElement, iAttrs, ngModel) {

            var editorUniqueId = nextId++;

            var newElement = $compile('<div>' +
                '<div class="wmd-panel">' +
                '<div id="wmd-button-bar-' + editorUniqueId + '"></div>' +
                '<textarea class="wmd-input" id="wmd-input-' + editorUniqueId + '">' +
                '</textarea>' +
                '</div>' +
                '<div id="wmd-preview-' + editorUniqueId + '" class="wmd-panel wmd-preview"></div>' +
                '</div>')(scope);

            iElement.html(newElement);

            var converter = new Markdown.Converter();

            var help = function () {
                // 2DO: add nice modal dialog
                alert("Do you need help?");
            }

            var editor = new Markdown.Editor(converter, "-" + editorUniqueId, {
                handler: help
            });

            editor.run();

            var $wmdInput = $("#wmd-input-" + editorUniqueId);


            // local -> parent scope change (model)
            $wmdInput.on('change', function () {
                //console.log('wmdInput changed', $wmdInput.val());
                var rawContent = $wmdInput.val();
                scope.$apply(function () {
                    ngModel.$setViewValue(rawContent);
                });
            });

            // parent scope -> local change
            scope.$watch(iAttrs.ngModel, function (value, oldValue) {
                console.log('ngModel changed', 'old: ', oldValue, 'new: ', value, editor, $wmdInput);
                // this does not really work, so we do it manually - what is the correct way?
                // scope.textareaValue = value;
                if (value !== undefined) {
                    /*scope.$apply(function(){
                        textareaValue = value;
                        editor.refreshPreview();
                    })*/
                    $wmdInput.val(value);
                    //console.log($wmdInput.html());
                    editor.refreshPreview(); // forces the editor to re-render the preview according to the contents of the input, e.g. after you have programmatically changed the input box content. This method is only available after editor.run() has been called.
                }

            });
        }
    }
});

1 个答案:

答案 0 :(得分:1)

您的代码很容易错过拼写错误;你的方法有错误的情况。

代码破碎:var converter = new Markdown.converter();

工作代码:var converter = new Markdown.Converter();