Javascript 从父控制器访问指令的属性
我有一个Javascript 从父控制器访问指令的属性,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我有一个datetime指令,它的作用域中包含两个属性:date和time。如何从父控制器访问这些属性?我试过这么做 <div id="inputs" ng-controller="InputCtrl"> Start: <datetime ng-model="start"></datetime> End: <datetime ng-model="end"></datetime> You have chosen
datetime
指令,它的作用域中包含两个属性:date
和time
。如何从父控制器访问这些属性?我试过这么做
<div id="inputs" ng-controller="InputCtrl">
Start: <datetime ng-model="start"></datetime>
End: <datetime ng-model="end"></datetime>
You have chosen to start on {{start.date}}, {{start.time}}
and end on {{end.date}}, {{end.time}}.
</div>
您应该将指令范围变量绑定到指令属性,如下所示:
angular.module("components", [])
.directive("datetime", function () {
return {
restrict: 'E',
scope: {
model: "=ngModel"
},
controller: function ($scope) {
$scope.model.time = "12:00 PM";
$scope.model.date = "12/01/2013";
},
template:
'<div><div class="control-group input-append">' +
'<input class="input-small" type="text" ng-model="date" data-date-format="mm/dd/yyyy" bs-datepicker>' +
'<button type="button" class="btn" data-toggle="datepicker"><i class="icon-calendar"></i></button>' +
'</div>' +
'<div class="control-group input-append">' +
'<input type="text" class="input-small" ng-model="time" bs-timepicker>' +
'<button type="button" class="btn" data-toggle="timepicker"><i class="icon-time"></i></button>' +
'</div></div>',
replace: true
};
});
角度模块(“组件”,[])
.指令(“日期时间”,函数(){
返回{
限制:'E',
范围:{
型号:“=ngModel”
},
控制器:功能($scope){
$scope.model.time=“下午12:00”;
$scope.model.date=“12/01/2013”;
},
模板:
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'',
替换:正确
};
});
“=ngModel”
表示您正在将隔离作用域中的model
绑定到父作用域中的ng model
变量。您是否使用隔离作用域定义指令?@davintroon我相信是的。请分享您的指令code@Ajaybeniwal请参见编辑。
angular.module("components", [])
.directive("datetime", function () {
return {
restrict: 'E',
scope: {
model: "=ngModel"
},
controller: function ($scope) {
$scope.model.time = "12:00 PM";
$scope.model.date = "12/01/2013";
},
template:
'<div><div class="control-group input-append">' +
'<input class="input-small" type="text" ng-model="date" data-date-format="mm/dd/yyyy" bs-datepicker>' +
'<button type="button" class="btn" data-toggle="datepicker"><i class="icon-calendar"></i></button>' +
'</div>' +
'<div class="control-group input-append">' +
'<input type="text" class="input-small" ng-model="time" bs-timepicker>' +
'<button type="button" class="btn" data-toggle="timepicker"><i class="icon-time"></i></button>' +
'</div></div>',
replace: true
};
});