Javascript 如何将值从控制器传递到指令
这是我的控制器Javascript 如何将值从控制器传递到指令,javascript,angularjs,Javascript,Angularjs,这是我的控制器 var controller = app.controller('ctrl', ['$scope', function ($scope) { $scope.months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var controller = app.controller('ctrl', ['$scope', function ($scope) {
$scope.months = ["January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"];
}]);
这是我的指令
app.directive('monthDirective', function () {
return {
restrict: 'A',
link: function (scope, elem) {
var fromDate , toDate;
scope.$watch('fromdate', function (newValue, oldValue) {
fromDate = new Date(newValue);
fromDate = moment(newValue, 'YYYY-MM-DD');
console.log('newValue', newValue)
});
scope.$watch('todate', function (newValue, oldValue) {
toDate = new Date(newValue);
toDate = moment(newValue, 'YYYY-MM-DD');
var range = moment.range(fromDate, toDate);
console.log('toDate', toDate)
range.by('months',function (moment) {
moment.toArray('months');
console.log('I am the array', moment.toArray('months'));
var mom = moment.toArray('months');
for (var i = 0; i <= scope.months.length; i++)
{
for (var j = 0; j <= mom.length;j++)
{
if(scope.months[i] == mom[j][1])
{
}
}
}
});
});
}
}
})
app.directive('monthDirective',function(){
返回{
限制:“A”,
链接:功能(范围、要素){
变量fromDate,toDate;
作用域:$watch('fromdate',函数(newValue,oldValue){
fromDate=新日期(newValue);
fromDate=时刻(新值“YYYY-MM-DD”);
console.log('newValue',newValue)
});
作用域.$watch('todate',函数(newValue,oldValue){
toDate=新日期(newValue);
toDate=力矩(新值,'YYYY-MM-DD');
var范围=力矩范围(从日期到日期);
console.log('toDate',toDate)
范围(月),功能(时刻){
今天的时刻(“月”);
console.log('我是数组',moment.toArray('月');
var mom=时间(月份);
对于(var i=0;i,虽然可以使用childscope或无范围,但最佳做法是使用:
用法:
<div month-directive months="months"></div>
尽管您可以使用childscope,也可以不使用scope,但最佳做法是使用:
用法:
<div month-directive months="months"></div>
默认情况下,该指令不创建子作用域。因此,默认情况下,您可以访问控制器的作用域:
app.controller('myController', function ($scope) {
$scope.test = 'test1';
});
app.directive('myDirective', function () {
return {
restrict: 'A',
link: function (scope, elem) {
console.log(scope.test)
}
}
})
但这是正确的。最佳做法是为指令使用隔离范围。因此,您可以通过在指令属性中传递它来访问months
您也可以阅读。这是关于范围继承的非常详细的说明。默认情况下,该指令不创建子范围。因此,您可以在默认情况下访问控制器的范围:
app.controller('myController', function ($scope) {
$scope.test = 'test1';
});
app.directive('myDirective', function () {
return {
restrict: 'A',
link: function (scope, elem) {
console.log(scope.test)
}
}
})
但这是正确的。最佳做法是为指令使用隔离范围。因此,您可以通过在指令属性中传递它来访问months
您也可以阅读。这是关于范围继承的非常详细的解释。您实际需要什么?访问父范围还是将变量传递给指令?我需要访问$scope.months(在控制器中)在directivescope中,您实际需要什么?访问父作用域或将变量传递给指令?我需要在directivescope中获得$scope.months的访问权限。也可以使用directivescope:true。@Silvinus如我所说,一个childscope(作用域:true)也可以使用,但是一种不好的做法为什么不好?这取决于您需要什么。如果您的指令依赖于父指令,则可以使用scope:true。如果指令可以单独使用,我同意您的看法。@Silvinus,因为这样它就不能被重用,除非您在父控制器中的每个位置将作用域属性命名为相同的你使用指令,这样就产生了一种紧密的耦合。正因为如此,我说我同意你的观点,只有在指令可以单独用于其他上下文的情况下。但在这种情况下,我们不知道用例……我只想说范围:true不是一种坏做法。这取决于你需要什么。范围:true也可以被使用。@Silvinus就像我说的,一个childscope(范围:true)也可以使用,但是一种不好的做法为什么不好?这取决于您需要什么。如果您的指令依赖于父指令,则可以使用scope:true。如果指令可以单独使用,我同意您的看法。@Silvinus,因为这样它就不能被重用,除非您在父控制器中的每个位置将作用域属性命名为相同的你使用指令,因此产生了一种紧密的耦合。正因为如此,我说我同意你的观点,只有在指令可以单独用于其他上下文的情况下。但在这种情况下,我们不知道用例……我只想说范围:true不是一种坏做法。这取决于你需要什么