首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angularjs:自定义双向绑定属性指令

Angularjs:自定义双向绑定属性指令
EN

Stack Overflow用户
提问于 2019-03-01 11:33:28
回答 2查看 291关注 0票数 0

我必须编写一个具有以下行为的指令:

主计长:

代码语言:javascript
复制
vm.mymodel = 'Hello World'

Html:

代码语言:javascript
复制
<custom-input mydirective="{propbind: 'data-value', propmodel: vm.mymodel}"></custom-input>

我希望将我的自定义输入转换为:

代码语言:javascript
复制
<custom-input data-value="{{vm.mymodel}}"></custom-input>
  • 当我从控制器更新vm.mymodel时,数据值属性必须更改。
  • 当我更新数据值时,vm.mymodel也必须更新。
  • 我不能对自定义输入使用ng-mmodel指令(它使用数据值,并具有应用于它的内部函数)。

所以我想做的是:

  • 在我的指令的编译函数中,将scope.propbind属性值设置为“{{propmodel}”,然后删除"mydirective“属性。但我似乎无法进入这里的范围。然而,如果我使用element.attr(' data-value ','hello world'),我可以看到我的数据值被正确设置。
  • 我试图在链接函数中执行同样的操作,但是我的作用域的值没有定义
  • 我也尝试了控制器函数,但我的作用域也是空的,而且我没有访问attr或元素的权限。

代码语言:javascript
复制
var app = angular.module('myApp', []);
app.controller('myctrl', function($scope) {
  var vm = this;
  this.mymodel = 'Hello world !';
});

app.directive('mydirective', function () {
      return {
        restrict: 'A',
        replace: false,
        transclude: false,
        scope: {
          bindingvalue: '='
        },
        compile: function(element) {
          //remove the element for clarity
          element.removeAttr('mydirective'); 
        },
        link: function($scope, elem, attr) {
          //set the attribute
          attr.$set($scope.bindingvalue.propbind, $scope.bindingvalue.propmodel);
          //Observe the attribute value for changes
          attr.$observe($scope.bindingvalue.propbind, function(value) {
            $scope.bindingvalue.propmodel = value;
          })
        },
        controller: function($scope, $element) {
          //Nothing here yet
        }
      }
    });
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myctrl as vm">
  <input mydirective="{propbind: 'data-value', propmodel: vm.mymodel}" type="text"/>
  model value : {{vm.mymodel}}
</div>

你能帮帮我吗?我的逻辑对我的问题有好处吗?我认为,如果我设法在我的链接函数中得到我的范围值,那么每个人的想法都可以工作。

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-01 14:33:06

我终于成功了。这是我的解决方案:

代码语言:javascript
复制
var app = angular.module('myApp', []);
app.controller('myctrl', function ($scope) {
  var vm = this;
  vm.mymodel = 'Hello world !';
  vm.changeModel = function() {
    vm.mymodel = 'New Value'
    console.log(document.getElementById('myinput'))
  }
  vm.changeAttribute = function() {
    document.getElementById('myinput').setAttribute('data-value', '123456789');
    console.log(document.getElementById('myinput'))
  }
});

app.directive('mydirective', function ($timeout, $interval) {
  return {
    restrict: 'A',
    replace: false,
    transclude: false,
    scope: {
      propbind: '@',
      model: '='
    },
    link: function (scope, elem, attr) {
      attr.$set(scope.propbind, scope.model);

      //Watch model for changes, and update attribute
      scope.$watch(function () {
        return scope.model
      }, function () {
        attr.$set(scope.propbind, scope.model);
      });

      //Watch for changes in attribute, and update model
      scope.$watch(function () {
        return elem[0].getAttribute(scope.propbind);
      }, function (value) {
        $timeout(function () {
          scope.model = value;
        });
      });

      //Get attribute value for angularjs to reevaluate dom
      $interval(function () {
        var val = elem[0].getAttribute(scope.propbind)
      }, 100);
    }
  }
});
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myctrl as vm">
  <input id="myinput" mydirective propbind="data-value" model="vm.mymodel" type="text"/>
  model value : {{vm.mymodel}}
  <div>
  <button ng-click="vm.changeModel()">change model value</button>
  <button ng-click="vm.changeAttribute()">change model value</button>
  </div>
  {{document.getElementById('myinput')}}
</div>

演示使用一个基本输入而不是我的自定义输入,因此它实际上并不绑定到数据值属性。但是,您可以单击这两个按钮来更新控制器中的模型或数据值属性。您可以看到,如果更改控制器中的值,则会更新数据值。另外,如果您更新输入的数据值,则在控制器中更新模型。

票数 0
EN

Stack Overflow用户

发布于 2019-03-01 12:15:06

这应该是可行的:

代码语言:javascript
复制
var app = angular.module('myApp', []);
app.controller('myctrl', function($scope) {
  var vm = this;
  vm.mymodel = 'Hello world !';
});

app.directive('customInput', function () {
      return {
        restrict: 'EA',
        scope: {
          value: '='
        },
        template: '<input type="text" ng-model="value" />',
        controller: function($scope, $element) {
          //Nothing here yet
        }
      }
    });
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myctrl as vm">
  <custom-input data-value="vm.mymodel"> </custom-input>
  model value : {{vm.mymodel}}
</div>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54943801

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档