javascript - 指令永远不会被称为angularjs

标签 javascript jquery html angularjs angularjs-directive

我们正在创建一个指令,以在用户单击按钮时增加和减少文本框中的数字。

这是我的自定义指令的代码。

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 View 中:-

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

(1) Console没有报错。

(2) 尝试将警报放在指令的顶部。没有警报消息出现。

提前致谢!

最佳答案

试试这个:

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

关于javascript - 指令永远不会被称为angularjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29384207/

相关文章:

css - 在 IE8 中使用 jquery 加载 css 的问题

javascript - jQuery 兼容性的最佳实践

javascript - 从大图像即时创建缩略图预览图像

html - 如何在 div 的中心对齐具有特定宽度的文本?

javascript - URL.createObjectURL 和字符集 UTF-8

javascript - 如何在 Typescript 1.8 中包含未类型化的节点模块?

javascript - 解析来自 REST API 调用的 JSON 响应

javascript - 无法让 Skrollr 与 BootStrap 3 一起使用 one page wonder

javascript - jQuery Accordion 问题

javascript - 使用 dojo 更改 <div> 的 CSS 类