Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 指令从未被称为angularjs_Javascript_Jquery_Html_Angularjs_Angularjs Directive - Fatal编程技术网

Javascript 指令从未被称为angularjs

Javascript 指令从未被称为angularjs,javascript,jquery,html,angularjs,angularjs-directive,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=

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

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

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');
        };
    }
};
} ]);
var CEDirectives=angular.module('App.customnumberdirectives',[])
.directive('ngcustomNumber',['$compile',function($compile){
var TEMPLATE_URL='/customnumber/index.html';
变量tem=''+
'  ' +
“公司”+
“十二月”+
'';
//设置此指令的默认模板
$templateCache.put(模板URL,tem);
返回{
限制:“E”,
范围:{
型号:“@”,
onblurevent:“&”
},
替换:正确,
templateUrl:函数(元素,属性){
返回attrs.templateUrl | | TEMPLATE_URL;
},
链接:功能(范围、元素、属性){
scope.onBlurHandler=函数(){
if(scope.onblurevent){
scope.onblurevent();
}
};
scope.Increase=函数(){
警报(“公司”);
};
scope.Decrease=函数(){
警报(“Dec”);
};
}
};
} ]);
在html视图中:-

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

(1) 控制台中没有错误

(2) 尝试将警报置于指令顶部。没有出现警报消息

提前谢谢

试试这个:

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

因为您的编译没有以正确的方式完成。您必须确保给定的作用域位于模板上。要检查它,您可以在模板中打印作用域id。正如您所知,每个作用域都有它的id,所以如果指令作用域和模板作用域id不相同,您的问题就在其中,解决方案是:

以正确的方式编译它。传入指令的范围。 $compile服务:$compile(模板)(directiveScope)


您可以控制范围,并将看到我所说的字段

您是否在HTML中应用了ngApp。它在HTML标记.App.customnumberdirectives中是您的模块名称,只是为了确认?您是否已将
'App.customnumberdirectives'
的引用\依赖项添加到您的主应用程序模块中。您的指令是否已加载到浏览器中?有效。但我可以知道是否必须以类似的方式编写指令吗?您已经设置了
restrict:'E'
,这意味着只允许使用元素。使用
AE
否则,如果您想使用as attributes,因为HTML不区分大小写,我们使用小写形式引用DOM中的指令,通常在DOM元素(例如ng模型)上使用破折号分隔的属性。