AngularJS Custom Directive Two Way Binding

前端 未结 5 1098
甜味超标
甜味超标 2020-12-29 08:17

If I have an AngularJS directive without a template and I want it to set a property on the current scope, what is the best way to do it?

For example, a directive tha

5条回答
  •  清歌不尽
    2020-12-29 09:09

    A great way to apply two way binding is to use directive components. Here is my solution. It allows use of ng-repeat and expandable data binding.

    View Plunker

    HTML

      
        Data: {{data}}
        

    Controller

    app.controller('MainCtrl', function($scope) {
      $scope.data = [];
      $scope.data[0] = 'Marco';
      $scope.data[1] = 'Billy';
    });
    

    Directive

    app.directive("mydirective", function(){
        return {
            restrict: "EA",
            scope: {name: '='},
            template: "
    Your name is : {{name}}
    "+ "Change your name : " }; });

    In the case of the counter, it can be done using the same method.

提交回复
热议问题