在指令中使用服务(angularjs)
我已经用angularjs构建了一个应用程序,并创建了菜单指令,这些指令根据加载的视图应用 这是我的菜单指令的结构:在指令中使用服务(angularjs),angularjs,dependency-injection,angularjs-directive,angularjs-service,Angularjs,Dependency Injection,Angularjs Directive,Angularjs Service,我已经用angularjs构建了一个应用程序,并创建了菜单指令,这些指令根据加载的视图应用 这是我的菜单指令的结构: angular.module('myApp') .directive('ogSection1Nav', function() { return { restrict: 'E', replace: true, templateUrl: '../scripts/directives/section1_nav.html', controller:
angular.module('myApp')
.directive('ogSection1Nav', function() {
return {
restrict: 'E',
replace: true,
templateUrl: '../scripts/directives/section1_nav.html',
controller: function($scope, $location) {
$scope.getClass = function(path) {
var cur_path = $location.path().substr(0, path.length);
if (cur_path === path) {
if($location.path().substr(0).length > 1 && path.length === 1 )
return "";
else
return "currentNav";
} else {
return "";
}
}
}
};
});
模板
<section class="adminMenu">
<nav class="side-nav">
<ul>
<li ng-class="getClass('/section1_summary')"><a href="#/section1_summary"><img title="Summary" src="images/summary.svg" title="Summary" height="25" /><span class="navText">Summary</span></a></li>
<li ng-class="getClass('/section1_spot_list')"><a href="#/section1_spot_list"><img title="Spot List" src="images/postList.svg" title="" height="25" /><span class="navText">Spot List</span></a></li>
<li ng-class="getClass('/section1_volume')"><a href="#/section1_volume"><img title="Volume" src="images/volumeHour.svg" title="" height="25" /><span class="navText">Volume</span></a></li>
<li ng-class="getClass('/section1_location')"><a href="#/section1_location"><img title="Location" src="images/geography.svg" title="" height="25" /><span class="navText">Location</span></a></li>
<li ng-class="getClass('/section1_media_type')"><a href="#/section1_media_type"><img title="Media Type" src="images/mediaType.svg" title="" height="25" /><span class="navText">Media Type</span></a></li>
</ul>
</nav>
</section>
然后,我在指令中调用getClass函数,如下所示:
angular.module('myApp')
.service('MenuState', function($location) {
this.getClass = function(path) {
var cur_path = $location.path().substr(0, path.length);
if (cur_path === path) {
if($location.path().substr(0).length > 1 && path.length === 1 )
return "";
else
return "currentNav";
} else {
return "";
}
};
});
angular.module('portalDashboardApp')
.directive('ogAdvertisingMediatracNav', function() {
return {
restrict: 'E',
replace: true,
templateUrl: '../scripts/directives/advertising_mediatrac_nav.html',
controller: function($scope, MenuState) {
$scope.getClass = MenuState.getClass();
}
};
});
但是,我得到了以下错误:TypeError:无法读取未定义的属性'length'
我认为这是因为path变量没有传递给我的服务,但我不确定如何通过查看我菜单模板的各个ng class=“getClass('/path_name')”>部分来获取我的路径,我确实认为问题出在您的服务中:
this.getClass = function(path) {
var cur_path = $location.path().substr(0, path.length);
if (cur_path === path) {
if($location.path().substr(0).length > 1 && path.length === 1 )
return "";
else
return "currentNav";
} else {
return "";
}
})
函数
getClass
需要一个变量“path”,并且在函数调用中不应用此变量$scope.getClass=MenuState.getClass()代码>我确实认为问题在于您的服务:
this.getClass = function(path) {
var cur_path = $location.path().substr(0, path.length);
if (cur_path === path) {
if($location.path().substr(0).length > 1 && path.length === 1 )
return "";
else
return "currentNav";
} else {
return "";
}
})
函数getClass
需要一个变量“path”,并且在函数调用中不应用此变量$scope.getClass=MenuState.getClass()代码>我确实认为问题在于您的服务:
this.getClass = function(path) {
var cur_path = $location.path().substr(0, path.length);
if (cur_path === path) {
if($location.path().substr(0).length > 1 && path.length === 1 )
return "";
else
return "currentNav";
} else {
return "";
}
})
函数getClass
需要一个变量“path”,并且在函数调用中不应用此变量$scope.getClass=MenuState.getClass()代码>我确实认为问题在于您的服务:
this.getClass = function(path) {
var cur_path = $location.path().substr(0, path.length);
if (cur_path === path) {
if($location.path().substr(0).length > 1 && path.length === 1 )
return "";
else
return "currentNav";
} else {
return "";
}
})
函数getClass
需要一个变量“path”,并且在函数调用中不应用此变量$scope.getClass=MenuState.getClass()代码>这可能对您有效,方法是将呼叫委派为:
angular.module('portalDashboardApp')
.directive('ogAdvertisingMediatracNav', function() {
return {
restrict: 'E',
replace: true,
templateUrl: '../scripts/directives/advertising_mediatrac_nav.html',
controller: function($scope, MenuState) {
$scope.getClass = function(path) {
return MenuState.getClass(path);
}
}
};
});
这可能会通过以下方式为您委派通话:
angular.module('portalDashboardApp')
.directive('ogAdvertisingMediatracNav', function() {
return {
restrict: 'E',
replace: true,
templateUrl: '../scripts/directives/advertising_mediatrac_nav.html',
controller: function($scope, MenuState) {
$scope.getClass = function(path) {
return MenuState.getClass(path);
}
}
};
});
这可能会通过以下方式为您委派通话:
angular.module('portalDashboardApp')
.directive('ogAdvertisingMediatracNav', function() {
return {
restrict: 'E',
replace: true,
templateUrl: '../scripts/directives/advertising_mediatrac_nav.html',
controller: function($scope, MenuState) {
$scope.getClass = function(path) {
return MenuState.getClass(path);
}
}
};
});
这可能会通过以下方式为您委派通话:
angular.module('portalDashboardApp')
.directive('ogAdvertisingMediatracNav', function() {
return {
restrict: 'E',
replace: true,
templateUrl: '../scripts/directives/advertising_mediatrac_nav.html',
controller: function($scope, MenuState) {
$scope.getClass = function(path) {
return MenuState.getClass(path);
}
}
};
});
@appdJava提出了一个很好的答案。或者,您也可以执行以下操作(无需调用MenuState的getClass,因此请删除括号):
@appdJava提出了一个很好的答案。或者,您也可以执行以下操作(无需调用MenuState的getClass,因此请删除括号):
@appdJava提出了一个很好的答案。或者,您也可以执行以下操作(无需调用MenuState的getClass,因此请删除括号):
@appdJava提出了一个很好的答案。或者,您也可以执行以下操作(无需调用MenuState的getClass,因此请删除括号):
是的,这是个问题,但我不确定如何将路径值传递到服务是的,这是个问题,但我不确定如何将路径值传递到服务是的,这是个问题,但我不确定如何将路径值传递到服务是的,这是个问题,但我不确定如何将路径值传递到服务太棒了!这就完成了,path变量现在被发送到我的serviceBrilliant中的getClass函数!这就完成了,path变量现在被发送到我的serviceBrilliant中的getClass函数!这就完成了,path变量现在被发送到我的serviceBrilliant中的getClass函数!这就完成了,path变量现在被发送到我的服务中的getClass函数,非常感谢这个解决方案。您能否解释一下$scope.getClass=MenuState.getClass;和$scope.getClass=MenuState.getClass()这两个函数的解释有什么不同?是的。不同之处在于,使用$scope.getClass=MenuState.getClass()调用的是无参数的MenuState服务的getClass函数,并将服务函数的返回值分配给$scope.getClass。这将在执行函数时产生错误,因为存在预期的“路径”参数。但是,如果执行$scope.getClass=MenuState.getClass,它只会将函数定义复制到$scope,而不会调用eit。它仅从html调用,其中ng class=“getClass('some path')”。在这里,它从$scope而不是MenuStateAha调用函数,这是有意义的。感谢扩展我的编码知识,非常感谢这个解决方案。您能否解释一下$scope.getClass=MenuState.getClass;和$scope.getClass=MenuState.getClass()这两个函数的解释有什么不同?是的。不同之处在于,使用$scope.getClass=MenuState.getClass()调用的是无参数的MenuState服务的getClass函数,并将服务函数的返回值分配给$scope.getClass。这将在执行函数时产生错误,因为存在预期的“路径”参数。但是,如果执行$scope.getClass=MenuState.getClass,它只会将函数定义复制到$scope,而不会调用eit。它仅从html调用,其中ng class=“getClass('some path')”。在这里,它从$scope而不是MenuStateAha调用函数,这是有意义的。感谢扩展我的编码知识,非常感谢这个解决方案。您能否解释一下$scope.getClass=MenuState.getClass;和$scope.getClass=MenuState.getClass()这两个函数的解释有什么不同?是的。不同之处在于,使用$scope.getClass=MenuState.getClass()调用的是无参数的MenuState服务的getClass函数,并将服务函数的返回值分配给$scope.getClass。这将在执行函数时产生错误,因为存在预期的“路径”参数。但是,如果执行$scope.getClass=MenuState.getClass,它只会将函数定义复制到$scope,而不会调用eit。它仅从html调用,其中ng class=“getClass('some path')”。在这里,它从$scope而不是MenuStateAha调用函数,这是有意义的。感谢扩展我的编码知识,非常感谢这个解决方案。您能否解释一下$scope.getClass=MenuState.getClass;和$scope.getClass=MenuState.getClass()这两个函数的解释有什么不同?是的。区别在于,,