Two way data binding in AngularJS Directives

前端 未结 3 1790
情深已故
情深已故 2020-12-08 10:10

I have been trying to define directives so I can display different \"widgets\" in a form, depending on the type of field and its parameters, which are stored in a database.

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-08 10:57

    I don't know why you are triggering the $apply method manually because you actually don't need it.

    I edited the example you used from the Angular page and included the input. It works for me: http://jsfiddle.net/6HcGS/2/

    HTML

    Title:

    JS

    function Ctrl3($scope) {
      $scope.title = 'Lorem Ipsum';
    }
    
    angular.module('zippyModule', [])
      .directive('zippy', function(){
        return {
          restrict: 'C',
          replace: true,
          transclude: true,
          scope: { title:'=zippyTitle' },
          template: '',
          link: function(scope, element, attrs) {
            // Your controller
          }
        }
      });
    

    UPDATE maxisam is right, you have to use ng-model instead of binding the variable against the value like so:

    
    

    Here is the working version: http://jsfiddle.net/6HcGS/3/

提交回复
热议问题