2017-07-07 69 views
0

我一直在为解决AngularJS问题而苦苦挣扎。我想加载自定义组件,然后调用它们的功能,但没有设法找到方法。如何从解析调用函数?

在下面的代码中,我试图在项目成功发布后显示SweetAlert(http://t4t5.github.io/sweetalert/)。

function config($ stateProvider,$ urlRouterProvider,$ ocLazyLoadProvider)$ urlRouterProvider.otherwise(“/ index/main”);

$ocLazyLoadProvider.config({ 
    // Set to true if you want to see what and when is dynamically loaded 
    debug: false 
}); 
$stateProvider 

    .... 
    .state('index.item', { 
     url: "/:itemId/edit", 
     templateUrl: "views/item-edit.html", 
     data: {pageTitle: 'Edit Item'}, 
     controller: function ($scope, $http, $stateParams, $rootScope, $state, $modal, $log, $ocLazyLoad) { 
      //console.log($routeParams.itemId); 

      var demo1 = function() { 
       SweetAlert.swal({ 
        title: "Welcome in Alerts", 
        text: "Lorem Ipsum is simply dummied text of the printing and typesetting industry." 
       }); 
      } 

      $scope.updateItem = function() { 
       $log.log('Submiting item info'); 
       $http.post('/api/item', $scope.item) 
        .success(function(data) { 
         demo1(); 
        }) 
        .error(function(data) { 
         console.log('Error: ' + data); 
        }); 
      } 
     }, 
     resolve: { 
      loadPlugin: function ($ocLazyLoad) { 
       return $ocLazyLoad.load([ 
        { 
         files: ['js/plugins/sweetalert/sweetalert.min.js', 'css/plugins/sweetalert/sweetalert.css'] 
        }, 
        { 
         name: 'oitozero.ngSweetAlert', 
         files: ['js/plugins/sweetalert/angular-sweetalert.min.js'] 
        } 
       ]); 
      } 
     } 
    }) 

在尝试使用上面的代码中,我得到以下错误:

ReferenceError: SweetAlert is not defined 
    at demo1 (config.js:156) 
    at config.js:170 
    at angular.js:9354 
    at angular.js:13168 
    at l.$eval (angular.js:14381) 
    at l.$digest (angular.js:14197) 
    at l.$apply (angular.js:14486) 
    at l (angular.js:9644) 
    at O (angular.js:9834) 
    at XMLHttpRequest.w.onload (angular.js:9775) 

其主要原因可能是物业loadPlugin这不叫控制器内的任何地方。任何人都知道如何修改代码来加载定义的插件并在控制器内部使用它们吗?

谢谢!

回答

2

该问题明确写入您的错误。

SweetAlert is not defined

这意味着在你的控制器,你忘了添加SweetAlert到你的依赖。

只需添加SweetAlert您conrtoller行的结尾:

controller: function ($scope, ... $ocLazyLoad, SweetAlert) { 
+0

我可以看到。我不知道的是如何加载包含SweetAlert定义的文件。 –

+0

@NikoGamulin增加了一个例子。 –