控制器" shahab"指令不起作用。(是否为空)

时间:2015-10-22 09:39:22

标签: angularjs angularjs-directive directive

我有两个指令,想要访问hossein指令中定义的控制器。

指令

app.directive('hossein', function() {
  return {
    restricted: 'A',
    require: '?ngModel',
    scope: {
      name: '=ngModel'
    },
    link: function(scope, element, attributes, ngModel) {
      console.log(",,,,,,,,,,,,,,,,,,,", scope.name);
    },
    controller: function($scope) {
      var name = $scope.name;
      this.fu = function() {
        return name;
      }
    }
  }
});
app.directive('shahab', function() {
  return {
    restricted: 'A',
    require: '^?hossein',
    scope: {
      name: '=ngName'

    },
    link: function(scope, element, attributes, controll) {
      console.log(",,,,,,,,gfddfdfg,,,,,,,,,,,", controll);
      scope.name = controll.fu();
    }
  }
});

标记

<div ng-app="app">
    <div ng-controller="maincontroller">
        <form ng-controller="newcontroller" name="myform" id="form">
            <h2>AngularJS</h2>
            <p>Name:
                <input name="user" type="text" hossein ng-model="name">
            </p>
            <p>Family:
                <input name="famil" type="text" shahab ng-name="">
            </p>
        </form>
    </div>
</div>

1 个答案:

答案 0 :(得分:3)

您必须使用嵌套的 shahab hossein 指令,以便可以访问外部控制器。

<hossein ng-model="name">

    <shahab ng-name="">

    </shahab>
</hossein>