1

例...プランカー

addon.js:

(function () {
  'use strict';
 angular.module('jzAddons', [])
  .factory('jzThemeFac', function () {
    return {
      themes:[
        {
          name: 'none',
          url: '',
          margin: '8px',
          bg: '#ffffff'
        },
        {
          name: 'amelia',
          url: '//netdna.bootstrapcdn.com/bootswatch/3.1.0/amelia/bootstrap.min.css',
          margin: '6px',
          bg: '#108a93'
        }
      ],

      changeTheme: function (theme) {
        var oldLink = angular.element('#bootstrapTheme');
        oldLink.remove();
        if (theme.name !== 'none') {
          var head = angular.element('head');
          head.append('<link id="bootstrapTheme" href="' + theme.url + '" rel="stylesheet" media="screen">');
        }
        currentTheme = theme;
      }
    };
  });

})();

app.js:

var app = angular.module('example', ['jzAddons']);

(function () {
  'use strict';

  app.controller('MainCtrl', ['jzThemeFac', function ($scope, jzThemeFac) {
    $scope.themes = jzThemeFac.themes;  /* Error comes from this line */
  }]);

})();

body タグの一番下に次のものがあります。

<script src="addon.js"></script>
<script src="app.js"></script>

エラーが発生し続けます:

TypeError: Cannot read property 'themes' of undefined
4

2 に答える 2

0

工場での注入が間違っていました。次のことができます。

app.controller('MainCtrl', ['$scope', 'jzThemeFac', function ($scope, jzThemeFac) {...

あるいは単に:

app.controller('MainCtrl', function ($scope, jzThemeFac) {

編集: 移動することもできます:

(function () {
    'use strict';  

ファイルの先頭まで

于 2014-03-23T19:28:23.813 に答える
0

$scope を MainCtrl に挿入することを忘れないでください。したがって、app.js は次のようになります。

var app = angular.module('example', ['jzAddons']);

(function () {
  'use strict';

  app.controller('MainCtrl', ['$scope', 'jzThemeFac', function ($scope, jzThemeFac) {
    $scope.themes = jzThemeFac.themes;  /* Error comes from this line */
  }]);

})();
于 2014-03-23T19:30:05.897 に答える