指令永远不会被调用 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>
因为你的编译没有正确完成 way.You 必须确保给定的范围是 ON template.For 检查它你可以在 template.As 中打印范围 ID 你知道每个范围都有它的ID,所以如果指令范围和模板范围id不一样,你的问题就在里面,解决方案是:
在你的指令范围内用正确的 way.passing 编译它。
$compile 服务:$compile(template)(directiveScope);
您可以控制示波器并将看到我正在谈论的字段
我们正在创建一个指令,以在用户单击按钮时增加和减少文本框中的数字。
这是我的自定义指令的代码。
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>
因为你的编译没有正确完成 way.You 必须确保给定的范围是 ON template.For 检查它你可以在 template.As 中打印范围 ID 你知道每个范围都有它的ID,所以如果指令范围和模板范围id不一样,你的问题就在里面,解决方案是:
在你的指令范围内用正确的 way.passing 编译它。 $compile 服务:$compile(template)(directiveScope);
您可以控制示波器并将看到我正在谈论的字段