2016-04-15 82 views
0

我有这种说法我怎样才能从父指令访问ngModel的输入/ angularjs

<div my-directive="somevalue"> 
    <input name="myField" ng-model="dataForMyField"> 
    </div> 

这是我的指令

app.directive('myDirective', function ($compile) { 
    return { 
    restrict: 'A', 
    template: `<div ng-transclude=""></div> 
      <div>SomeValue from directive <strong>{{ someReturnedValue }}</strong></div>`, 
    transclude: true, 
    controller: function($scope, $element, $attrs, $transclude) { 

     $scope.someReturnedValue = 'ValueFromDirective'; 
     console.log('Name of input'); // myField 
     $scope.$watch('vm.ngModel', function(newValue, oldValue, scope) { 
      console.log('WOW! Input.ngModel changed', newValue); // world in the init 
     }); 

    } 
    } 
}) 

如何访问输入的ngModel。

---------这里>是plkr:http://plnkr.co/edit/nWNAuf9jbv0sgY2VYRtZ?p=preview

回答

0

试试这个

<div my-directive="somevalue"> 
    <input name="myField" ng-model="dataForMyField"> 
</div> 


app.directive('myDirective', function() { 
    return { 
    scope: { 
     dataForMyField: '@dataAttr' 
    }, 
    restrict: 'A', 
    template: `<div ng-transclude=""></div> 
      <div>SomeValue from directive <strong>{{ someReturnedValue }}</strong></div>`, 
    transclude: true, 
    controller: function($scope, $element, $attrs, $transclude) { 

     $scope.someReturnedValue = 'ValueFromDirective'; 
     console.log('Name of input'); // myField 
    } 
    } 
})