作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个由 <input>
组成的自定义组件(指令)盒子和一个 [-]
和 [+]
纽扣。目前,下面的例子只实现了输入框。
所以,假设我的指令有以下 HTML:<my-input ng-blur="onBlur($event)" ng-focus="onFocus($event)"></my-input>
出于测试目的,我使用以下代码:
app.run(function ($rootScope) {
$rootScope.onBlur = function ($event) {
console.log('onBlur', $event);
};
$rootScope.onFocus = function ($event) {
console.log('onFocus', $event);
};
});
<my-input>
具有
<input>
的指令模板上的框,我需要
ng-blur
和
ng-focus
设置在
<my-input>
响应输入框上的模糊/焦点事件。
$event
好像是
undefined
我不明白为什么。想法?
最佳答案
好的,想通了。多伦的回答是一个很好的研究起点,但现在我想我已经找到了你想要的东西。关键是你必须使用 &
在链接部分,以使其执行表达式。
.directive('myInput', function($timeout) {
return {
restrict: 'E',
scope: {
data: '=',
blur: '&myBlur' //this is the key line
},
template: '<input ng-blur="blur($event)" ng-model="data">'
}
})
<my-input my-blur="runBlurFunc()"></my-input>
$scope.$root.onBlur()
而不是
runBlurFunc()
关于angularjs - 如何将 ngFocus/ngBlur 委托(delegate)给指令的模板 <input> 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31948142/
我是一名优秀的程序员,十分优秀!