如何从父指令/ angularjs访问输入的ngModel

时间:2016-04-15 11:58:23

标签: angularjs angularjs-directive typescript

我有这个观点

  <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。

---------&GT;这是一个plkr:http://plnkr.co/edit/nWNAuf9jbv0sgY2VYRtZ?p=preview

1 个答案:

答案 0 :(得分: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
    }
  }
})