簡體   English   中英

指令永遠不會被稱為angularjs

[英]Directive never gets called angularjs

當用戶單擊按鈕時,我們正在創建一個指令來增加和減少文本框中的數字。

這是我的自定義指令的代碼。

var CEDirectives = angular.module('App.customnumberdirectives', [])
.directive('ngcustomNumber', ['$compile', function ($compile) {
var TEMPLATE_URL = '/customnumber/index.html';
var tem = '<div class="wrapper">' +
          '  <input type="text" data-ng-model="{{model}}" data-ng-blur="onBlurHandler()">' +
          '  <button ng-click="Increase()" style="cursor:pointer; background-color: transparent">INC</button>' +
          '  <button ng-click="Decrease()" style="cursor:pointer; background-color: transparent">DEC</button>' +
          '</div>';

// Set the default template for this directive
$templateCache.put(TEMPLATE_URL,tem);

return {
    restrict: "E",
    scope: {
        model: '@',
        onblurevent: '&'
    },
    replace: true,
    templateUrl: function (element, attrs) {
        return attrs.templateUrl || TEMPLATE_URL;
    },
    link: function (scope, element, attributes) {

        scope.onBlurHandler = function () {
            if (scope.onblurevent) {
                scope.onblurevent();
            }
        };

        scope.Increase = function () {
            alert('Inc');
        };
        scope.Decrease = function () {
            alert('Dec');
        };
    }
};
} ]);

在html視圖中:-

 <ngcustomNumber model="weight" onblurevent="Save"></ngcustomNumber>

(1)控制台無錯誤。

(2)嘗試在指令頂部放置警報。 沒有警告消息出現。

提前致謝!

嘗試這個:

<ngcustom-number model="weight" onblurevent="Save"></ngcustom-number>

因為編譯不正確,必須確保給定范圍是ON模板。要檢查它,可以在模板中打印范圍ID。您知道每個范圍都有它的ID,所以如果指令范圍和模板范圍ID是不一樣,您的問題出在其中,解決方案是:

以正確的方式進行編譯。將指令范圍傳遞給它。 $ compile服務:$ compile(template)(directiveScope);

您可以控制范圍並看到我正在談論的字段

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM