如何从父指令/angularjs访问输入的ngModel
我有这个看法如何从父指令/angularjs访问输入的ngModel,angularjs,angularjs-directive,typescript,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 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
});
}
}
})
app.directive('myDirective',函数($compile){
返回{
限制:“A”,
模板:`
指令{{someReturnedValue}`中的SomeValue,
是的,
控制器:函数($scope、$element、$attrs、$transclude){
$scope.someReturnedValue='ValueFromDirective';
console.log('Name of input');//myField
$scope.$watch('vm.ngModel',函数(newValue、oldValue、scope){
log('WOW!Input.ngModel changed',newValue);//init中的world
});
}
}
})
如何访问输入的模型
--------->这里有一个plkr:试试这个
<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
}
}
})
app.directive('myDirective',function(){
返回{
范围:{
dataForMyField:“@dataAttr”
},
限制:“A”,
模板:`
指令{{someReturnedValue}`中的SomeValue,
是的,
控制器:函数($scope、$element、$attrs、$transclude){
$scope.someReturnedValue='ValueFromDirective';
console.log('Name of input');//myField
}
}
})