我们正在创建一个指令,以在用户单击按钮时增加和减少文本框中的数字。
这是我的自定义指令的代码。
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/