简体   繁体   English

指令永远不会被称为angularjs

[英]Directive never gets called angularjs

We are creating a directive to Increase and Descrease a number in a text box when user clicks on the buttons. 当用户单击按钮时,我们正在创建一个指令来增加和减少文本框中的数字。

Here is the code of my custom 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');
        };
    }
};
} ]);

In the html view:- 在html视图中:-

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

(1) No error in Console. (1)控制台无错误。

(2) Tried putting alert on the top of the directive. (2)尝试在指令顶部放置警报。 No alert message comes up. 没有警告消息出现。

Thanks in advance! 提前致谢!

尝试这个:

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

Because your compile is not done with right way.You must be sure that given scope IS ON template.For checking it you can print scope id in template.As you know every scope has it's ID,so if directive scope and template scope id is not the same,your problem is in it,and the solution is: 因为编译不正确,必须确保给定范围是ON模板。要检查它,可以在模板中打印范围ID。您知道每个范围都有它的ID,所以如果指令范围和模板范围ID是不一样,您的问题出在其中,解决方案是:

to compile it with right way.passing in it your directive's scope. 以正确的方式进行编译。将指令范围传递给它。 the $compile service: $compile(template)(directiveScope); $ compile服务:$ compile(template)(directiveScope);

you can console the scope and will see the fields I'm talking about 您可以控制范围并看到我正在谈论的字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM