如何在ngRepeat中调用指令
How can I call an Directive inside ngRepeat?
当我试图在ngRepeat内部调用指令时失败了。但如果在ngRepeat外部调用相同的指令,则工作正常。
请检查以下代码:
<html>
<head>
<script src ="//ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body ng-app = "myApp">
<div ng-controller = "myCtrl">
<div ng-repeat="list in lists">
<div>{{fileName}}</div>
<input type = "file" id=list file-model = "myFile"/>
<button ng-click = "uploadFile()">upload me</button>
</div>
</div>
<script>
var myApp = angular.module('myApp', []);
myApp.directive('fileModel', ['$parse', function ($parse) {
return {
restrict: 'E',
link: function($scope, element, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
element.bind('change', function(){
$scope.$apply(function(){
modelSetter($scope, element[0].files[0]);
});
});
}
};
}]);
myApp.controller('myCtrl', ['$scope', function($scope){
$scope.lists=[1,2,3];
$scope.uploadFile = function(){
var file = $scope.myFile;
console.log('file is ' );
console.dir(file);
$scope.fileName=file.name;
/* var uploadUrl = "/fileUpload";
fileUpload.uploadFileToUrl(file, uploadUrl);*/
};
}]);
</script>
</body>
</html>
如何从ng repeat调用指令?
您将指令限制为'E'元素,并将其用作属性。将restrict属性更改为EA。
var myApp = angular.module('myApp', []);
myApp.directive('fileModel', ['$parse', function ($parse) {
return {
restrict: 'EA',
link: function($scope, element, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
element.bind('change', function(){
$scope.$apply(function(){
modelSetter($scope, element[0].files[0]);
});
});
}
};
}]);
相关文章:
- 显示指令时调用指令方法
- AngularJS:如何调用指令中定义的函数'控制器的作用域
- 如何在ngRepeat中调用指令
- 未对输入更改调用指令
- 从子指令调用指令函数不起作用
- 从 AngularJS 中的父控制器调用指令的方法
- 从服务 AngularJS + TypeScript 调用指令
- 如何从 Angular 上的控制器调用指令的函数
- 如何从 JavaScript 调用指令函数
- Angular JS 如何从指令模板内容事件调用指令范围函数
- 从控制器调用指令中函数的最佳方法是什么
- 从传入的内容调用指令方法
- Angularjs-在ng-init函数完成后调用指令
- 从另一个指令调用指令
- 从父指令角度调用指令函数
- 如何从控制器调用指令作用域上的函数
- 有棱角的从html调用指令中的函数
- 动态元素未在AngularJS上调用指令方法
- 是'不良做法'用于Angular控制器调用指令上的函数
- AngularJS:从具有隔离作用域的控制器中调用指令中的函数