可重复使用的按钮指令不起作用
Reusable button directive not working
Hi可重复使用按钮指令不起作用,
这里我创建了可重复使用的按钮指令,在下面的代码中,静态按钮可以工作,但动态(指令)按钮不工作。请检查下面的代码
感谢您的帮助
var app = angular.module('testApp', [ ]);
app.directive('telSavebutton', function() {
return{
restrict: 'E',
template: '<button type="submit" ng-click="testcontroller()" >directive button</button>',
transclude: true,
scope: {
onSubmit: '&',
likeClick: '&'
},
link: function(scope, element, attributes){
scope.submit = function(){
scope.onSubmit();
}
}
}
});
app.controller('testCntler', ['$scope', '$location', function ($scope, $location) {
$scope.testcontroller=function()
{
alert("Working")
}
}]);
<script data-require="angular.js@~1.3.15" data-semver="1.3.15"
src="https://code.angularjs.org/1.3.15/angular.js"></script>
<body class="hold-transition skin-blue sidebar-mini" data-ng-app="testApp" >
<form novalidate="novalidate" ng-submit="vm.onSubmit()"
ng-controller="testCntler" >
<table width="293" border="0">
<tr>
<td width="127">First Name</td>
<td width="150"> <input type="text" ng-model="fname" ></td>
</tr>
<tr>
<td>Mid Name</td>
<td> <input type="text" ng-model="mName" ></td>
</tr>
<tr>
<td>Last Name</td>
<td> <input type="text" ng-model="lName" ></td>
</tr>
<tr>
<td colspan="2"> <button type="submit"
ng-click="testcontroller()" >Static button</button>
<tel-Savebutton check-Id="firstName" ></tel-Savebutton></td>
</tr>
</table>
</form>
</body>
您的问题是telSavebutton
指令具有隔离作用域,但您在该指令的模板中使用了函数testcontroller()
,该函数在Controller的作用域中。
通过首先修改你的模板来修复它:
template: '<button type="submit" ng-click="submit()" >directive button</button>',
通过传递一个函数作为指令实例的属性:
<tel-savebutton on-submit="testcontroller"></tel-savebutton></td>
更改指令中的ng-click="testcontroller()":
template: '<button type="submit" ng-click="onFormSubmit()" >directive button</button>'
并在html:中添加提交
<tel-Savebutton check-Id="firstName" on-form-submit="testcontroller()"></tel-Savebutton></td>
同时更改范围参考:
scope: {
onFormSubmit: '&'
},
相关文章:
- 角度指令:使提交按钮指令侦听表单验证
- 带有按钮angularjs的captcha指令
- 如何在单击按钮时动态添加指令
- AngularJS指令模拟单选按钮
- 使用带有角度的按钮显示和隐藏指令
- AngularJs 指令在更改时获取单选按钮 ng-model 值
- 如何在角度 js 中单击按钮时引用指令
- Angularjs ng-repeat中的Angleularjs按钮加载状态指令
- 可重复使用的按钮指令不起作用
- 如何在Angularjs中使用按钮点击指令创建选择选项
- AngularJS指令用于用户协议视图,检测启用按钮,不适用于移动safari
- 如何创建AngularJS指令以在按下回车按钮时提交jquery对话框
- AngularJS-通过指令向按钮添加onClick事件
- 指令来确认AngularJS中的delete按钮
- Angularjs-当dom操作被分配给按钮点击时,它是有效的,在指令中它是不完整的
- 使用Angular指令向DOM中添加可切换按钮
- 创建层叠所有属性的条件按钮指令
- 打开ngDialog按钮,点击from指令
- 指令创建[下载]按钮
- 无法在单击单个按钮时删除使用指令创建的表的选定行