gpt4 book ai didi

javascript - $scopeProvider 的问题

转载 作者:行者123 更新时间:2023-11-28 00:03:14 25 4
gpt4 key购买 nike

我有一个如下所示的 js 文件。

(function(){

angular.module("MyModule",[])
.service('testService',testService)
.directive("testDirective",testDirective);;

function testDirective(testService){
var directive = {
template: '<input type="text">',
restrict: 'EA'
};
return directive;
}

function testService($scope){
$scope.name = "testName";
}

}())

当我加载页面时,我收到如下错误。

Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- inputResourceService <- inputResourceDirective

请帮我解决这个问题。

最佳答案

$scope dependency will never be available inside service function

您应该在服务中创建一个变量,以便在所有 Angular 组件之间轻松共享

(function(){

angular.module("MyModule",[])
.service('testService',testService)
.directive("testDirective",testDirective);;

function testDirective(testService){
var directive = {
template: '<input type="text" ng-model="something">',
restrict: 'EA',
link: function(scope, element, attrs){
scope.name = testService.name; //assigned service variable value to scope
}
};
return directive;
}

function testService(){
var testService = {}
testService.name = "testName";
return testService;
}

}())

Working Plunkr

关于javascript - $scopeProvider 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31594007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com