angular应用,一个input上有自定义directive和ng-model,怎么在directive更新input的value时更新model



 wise.directive('upload', function () {
    return {
        link: function ($scope, iElement, iAttrs, controller) {
            iElement.upload({
                success: function(data, self) {
                    $scope[iAttrs['ngModel']]=data;
                    $scope.$apply();
                },
            });
        }
    };
});

如上的代码在


 <input type="text" class="form-control" upload ng-model="pic_pad">

中可用,但是变为这样


 <input type="text" class="form-control" upload ng-model="scenetopic.pic_pad">

怎么做?
求优雅的解决方法。

HTML angularjs JavaScript

御門小路樣 11 years, 11 months ago

用 $parse。 官方文档


 wise.directive('upload', function ($parse) {
    return {
        link: function ($scope, iElement, iAttrs, controller) {
            iElement.upload({
                success: function(data, self) {
                    $parse(iAttrs['ngModel']).assign($scope, data);
                    $scope.$apply();
                },
            });
        }
    };
});

十六夜小夜 answered 11 years, 11 months ago

Your Answer