programing

각진 쿠키는 왜 주입할 수 없는 거죠?

golfzon 2023. 3. 5. 10:50
반응형

각진 쿠키는 왜 주입할 수 없는 거죠?

있습니다

<body ng-app="myApp">
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular-cookies.min.js">
    </script>
</body>

모든 것이 올바르게 로딩됩니다.

그 후 javascript에서 ngCookies를 삽입하려고 합니다.

angular.module("myApp", ["ngCookies"]).
    config(function($cookies) {
         console.log($cookies.myCookie);
    });

하지만 $cookie를 찾을 수 없는 것 같습니다.

 Unknown provider: $cookies from myApp

귀사의 기능적 활용 사례는 정확히 알 수 없지만 서비스를 투입할 수 없습니다.$cookies는 설정 블록내의 서비스입니다.설정 블록 내에 주입할 수 있는 것은 상수와 공급자뿐입니다.

실행 블록에 서비스를 주입할 수 있지만 쿠키로 무엇을 하려는지 잘 모르기 때문에 도움이 될지는 모르겠습니다.

BTW: 메인 앵귤러 파일 버전이랑ngCookies모듈.이것은 당신의 문제와 직접 관련이 없지만, 이것은 다소 이상합니다.

수동으로 주입할 수 있습니다.

myApp.config(function() {
  var $cookies;
  angular.injector(['ngCookies']).invoke(['$cookies', function(_$cookies_) {
    $cookies = _$cookies_;
  }]);

  // here you can use $cookies as usual
});

당신은 왜 우리가 특정해야 하는지 궁금할 수 있다.ngCookies에게injector()와 마찬가지로 을 호출하다.invoke()전화할 수 있나요?

ngCookies모듈 이름입니다(이 모듈을 사용하려면 프로젝트에 angular-module.module이 필요합니다).호출을 통해 인젝터를 작성하는 경우injector()이러한 모듈의 서비스를 사용할 수 있도록 사용하는 모듈을 지정합니다.

invoke()함수를 호출합니다만, (다양한 모듈에 의해 제공되는) 어떤 서비스를 함수에 전달해야 하는지 지정합니다(이 경우 제공되고 있는 서비스).ngCookies모듈 이름 지정$cookies)

이 솔루션은 Angular 앱이 자동으로 생성 및 사용하는 인젝터와 별도로 수동으로 새 인젝터를 생성하기 때문에 다소 번거롭지만 안전할 것입니다.ngCookie자체 상태를 유지하지 않고 브라우저 기능의 얇은 래퍼인 angular 기능만 사용하고 있는 것 같습니다.

같은 문제가 발생했습니다.앵귤러 버전과 앵귤러 쿠키 버전이 일치하지 않았습니다. 수정하기 위해 제가 한 일은 bower.json을 둘 다 동작하는 버전으로 업데이트한 것입니다.

"angular": ">=1.2.*",
"angular-cookies": ">=1.2.*"

그리고 뛰었습니다.

bower install

이런 질문을 받았어요

Unable to find a suitable version for angular, please choose one:
1) angular#1.3.13 which resolved to 1.3.13 and is required by angular-cookies#1.3.13 
2) angular#>=1.2.x <=1.4.x which resolved to 1.3.16 and is required by oclazyload#1.0.1 
3) angular#1.4.0 which resolved to 1.4.0 and is required by angular-cookies#1.4.0 
4) angular#>=1.2.* which resolved to 1.4.0 and is required by hashve 
5) angular#>=1 which resolved to 1.4.0 and is required by angular-bootstrap#0.11.2 
6) angular#>=1.2.26 <=1.5 which resolved to 1.4.0 and is required by angular-translate#2.7.2 
7) angular#~1.x which resolved to 1.4.0 and is required by restangular#1.5.1 
8) angular#^1.2.6 which resolved to 1.4.0 and is required by angular-socket-io#0.6.1 
9) angular#>= 1.0.8 which resolved to 1.4.0 and is required by angular-ui-router#0.2.15 
10) angular#>=1.2.0 <1.5.0 which resolved to 1.4.0 and is required by angular-moment#0.10.1Prefix the choice with ! to persist it to bower.json

그리고 3개를 골랐어요.

그리고 그것은 성공하였다.

https://stackoverflow.com/a/18971123/132374,에서 서비스 모듈의 'ngCookies'를 삭제해야 할 것 같습니다.

var serviceModule = angular.app('App.services', ['ngCookies']);

나한테는 효과가 있었어.주의해 주세요$cookies는 Angular에서 서비스를 주입할 수 있는 경우에만 작동합니다(자세한 내용은 승인된 답변을 참조하십시오).

다른 모든 사람들이 말했듯이, 당신은 앵귤러 쿠키 서비스를 사용할 수 없습니다.그러나 단순히 document.cookie를 사용하여 직접 액세스하고 해석할 수 없다는 법은 없습니다.또는 쿠키에 액세스할 수 있는 자체 공급자를 만들고 이를 주입할 수 있습니다.angular는 단순한 프레임워크이며 프레임워크에 제한이 있을 경우 javascript라는 것을 사용해야 한다는 점을 기억하십시오.

을 붙여야 .$cookieProvider, ★★★★★★★★★★★★★★★★」$cookies:

angular.module("MyApp", ["ngCookies","ngRoute"])
    .config(["$routeProvider", "$locationProvider","$cookiesProvider",
        function($routeProvider, $locationProvider, $cookies) {

        }
    ]);

언급URL : https://stackoverflow.com/questions/15358029/why-am-i-unable-to-inject-angular-cookies

반응형