2016-04-16 4 views
0

myService.jsでapp.jsの定数「appSettings」にアクセスするにはどうすればよいですか?ここでサービスからアプリケーションの定数にアクセスできません

app.js

(function() { 
    'use strict'; 
    angular 
     .module('myApp', ['ngMaterial']) 
     .constant('appSettings', { 
      someValue: 123 
     }) 
})(); 

私のサービス/ myService.jsある

(function() { 
    'use strict'; 
    angular 
     .module('myApp') 
     .factory('myService', ['appSettings', MyService]); 

    function MyService(appSettings) { 
     console.log(appSettings.someValue) 
    } 
})(); 

ブラウザのコンソールはappSettings.someValue

+0

使用 'angularobj.value' –

+0

は、あなたが直面している問題は何? –

+0

'MyService'関数はどこに定義されていますか?それはタイプミスですか? –

答えて

1

undefined`あなたが直面している問題が何であるかわからないと言うが、私はその値にアクセスすることができます。

(function() { 
 
    'use strict'; 
 
    angular 
 
    .module('myApp', []) 
 
    .constant('appSettings', { 
 
     someValue: 123 
 
    }) 
 
})(); 
 

 
(function() { 
 
    'use strict'; 
 
    angular 
 
    .module('myApp') 
 
    .factory('myService', ['appSettings', MyService]); 
 

 
    function MyService(appSettings) { 
 
    console.log(appSettings.someValue); 
 
    alert(appSettings.someValue); 
 
    
 
    return { 
 
     foo: function() {} 
 
    } 
 
    } 
 

 
    angular.module("myApp") 
 
    .controller("sa", function($scope, myService) { 
 
     myService.foo(); 
 
    }) 
 
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 
<div ng-app="myApp" ng-controller="sa"></div>

+1

私は依存性注入にいくつか問題があった;) – juleee

関連する問題