如何通过Angular中的$resource发送来自自定义指令的输入值
How to send input value from custom directive through $resource in Angular?
我已经找到了在自定义指令中使用ngModel的答案,我理解这个概念,但我不确定我是否理解如何在使用$resource时实现它。
我成功地将"文件"作用域注入到指令中,并进行了api调用,但返回到服务器的值为null。我确信Angular指令的实现是我的错所在。
指令.js
angular.module('pro').directive('buyLinkBox', function() {
return {
restrict: "AE",
replace: true,
template: '<md-input-container md-no-float md-block><label style="font-size:2.2vh">Buy Link for {{file.filename}}</label><input type="text" ng-blur="sendLink()" ng-model="file.buyLink" name="buyLink" id="buyLink"/></md-input-container>',
scope: {
file: '=',
},
controller: function($scope, $route ,BuyLinkService){
$scope.sending = BuyLinkService.get({
FileId: $scope.file._id
});
$scope.sendLink = function() {
$scope.sending.$sendLink(function() {
$route.reload();
}, function(errorResponse) {
alert('nope');
});
};
}
}
});
html
<buy-link-box file="file"></buy-link-box>
经过一番推导,我找到了一个解决方案。
我将ngModel添加到指令范围中,并将其包含在我的html中。但主要的问题是,我的资源在得到我的数据之前就被调用了。修复当然很简单。在启动PUT api调用之前,我创建了一个回调并将所需字段设置为输入变量。
希望这能帮助到别人。
指令.js
angular.module('pro').directive('buyLinkBox',['BuyLinkService', '$route',
function(BuyLinkService, $route) {
return {
restrict: "E",
replace: true,
template: '<md-input-container md-no-float md-block><label style="font-size:2.2vh">Buy Link for {{file.filename}}</label><input type="text" ng-blur="sendLink()" ng-model="ngModel" name="buyLink" id="buyLink"/></md-input-container>',
scope: {
file: '=',
ngModel: '='
},
link: function(scope, element, attrs){
scope.sendLink = function() {
BuyLinkService.get({MixedId: scope.file._id}, function(data){
data.buyLink = scope.file.buyLink;
data.$sendLink(function(file) {
alert(file.buyLink);
$route.reload();
}, function(errorResponse) {
alert('nope');
});
})
}
}
}
}]);
html
<buy-link-box ng-model="file.buyLink" file="file"></buy-link-box>
相关文章:
- 如何在自定义指令中获取计算的属性
- 向动态生成的DOM添加Angular自定义指令
- 自定义指令中的AngularJS ng模型
- 如何在html中以角度显示自定义指令的作用域
- 自定义指令模板中的AngularJS控制变量
- AngularJS通过自定义指令传递点击的项目
- 如何在angularjs中将多个作用域变量传递到自定义指令中
- 处理角度自定义指令中的onReady事件
- 为什么ng中的自定义指令在循环完成后重复运行
- 对于使用传递的数据计算的局部范围变量,角度绑定在自定义指令中不起作用
- Angular.JS自定义指令;不起作用
- 如何通过Angular中的$resource发送来自自定义指令的输入值
- AngularJS:如何访问自定义指令中的input[date]min属性
- AngularJS:ngRepeat作用域在具有隔离作用域和ngTransclude的自定义指令中失败
- 如何使用其他自定义指令中的元素标记作为另一个自定义指令的模板
- 如何限制自定义指令的可能属性值
- 动态网格作为自定义指令
- AngularJS - 在自定义指令中继承ngModel
- 调用另一个自定义指令的角度自定义指令
- 在 AngularJs 中验证自定义指令(我想让它成为必需的)