【问题标题】:Pass services in a $scope function in angular以角度在 $scope 函数中传递服务
【发布时间】:2016-07-19 14:05:58
【问题描述】:

模型 myApp 之前在文件 app.js 中定义,并且可以识别实际的文件 synchro.js。 我定义了一个控制器、一个服务和一个包含在控制器中的功能的 HTML 页面。
Controller 函数中的 $scope.clearcache 函数也无法识别我的个人服务 afficheModal 中的服务 ($http,$timeout,..)。 如果我放一个console.log(afficheModal),它会返回:undefined。

    (function () {
    'use strict';

    angular
        .module('myApp')
        .controller('SyncPointage.IndexController', Controller);

    function Controller($scope,$http,LS,$q,afficheModal,$rootScope,$timeout) {
        var vm = this;
        console.log('a',afficheModal)
        $rootScope.$on('us-spinner:spin', function (event, key) {
            $scope.spinneractive = true;
        });

        $rootScope.$on('us-spinner:stop', function (event, key) {
            $scope.spinneractive = false;
        });


         $scope.clearCache= function() {
            localStorage.removeItem('employees');
            localStorage.removeItem('working_profile');
            localStorage.removeItem('employee_working_profile');
            localStorage.removeItem('societes');
            localStorage.removeItem('etablissements');
            localStorage.removeItem('services');
            localStorage.removeItem('timestamp');
            var message = "Le cache est vidé";
            afficheModal.modal1($scope,$timeout,message)
        }
    }

})();

HTML:

<div class="col-md-16 main center-block">
    <div class="btn-group-vertical" role="group"></div>
      <button type="submit" class="btn btn-blue" ng-click="clearCache()"><i class="fa fa-trash fa-2x" aria-hidden="true" fa-5x></i>&Eacute;tape 1 : Vider le cache</button>
      <button type="submit" class="btn btn-blue" ng-click="onLoad(readData)"><i class="fa fa-spinner fa-2x" aria-hidden="true" fa-5x></i>&Eacute;tape 2 : Synchro Locale</button>
      <button type="submit" class="btn btn-blue" ng-click="lanceLocalData()"><i class="fa fa-database fa-2x" aria-hidden="true" fa-5x></i>&Eacute;tape 3 : Synchro Database</button>
  </div>

【问题讨论】:

  • 不清楚代码,因为缺少一些代码。你能提供一个工作小提琴吗?
  • 浏览器控制台有错误吗?如果是这样,请发布它。在大多数情况下,任何与角度相关的代码都存在错误,因此变量没有正确注入。
  • 这是我代码的一部分,因为它是一个非常大的项目。 1) 我之前在 app.js 文件中定义了 myApp 模块 2) 我在控制台中的错误是: angular.js:12477 TypeError: Cannot read property 'modal1' of undefined at Scope.Controller.$scope.clearCache (index.controller .js:39) 在 fn (eval at compile (angular.js:13322), :4:425) at callback (angular.js:23549) at Scope.$eval (angular.js:15989) at Scope .$apply (angular.js:16089) 在 HTMLButtonElement. (angular.js:23554) 在 HTMLButtonElement.dispatch (jquery.js:4435)

标签: angularjs scope timeout


【解决方案1】:

注入控制器的服务在视图中不可用,除非您将其分配给范围模型。它们也不是“全局变量”

你有

ng-click="clearCache(LS,afficheModal)"

这两个参数都显示为注入服务,但它们不是模型属性

你说过

试试

ng-click="clearCache()"

【讨论】:

  • 我在 html 中更正了,这是不匹配的。在函数控制器中,我传递参数。函数控制器 ($scope,$http,LS,$q,afficheModal,$rootScope,$timeout) {
猜你喜欢
  • 1970-01-01
  • 2018-01-28
  • 2016-02-27
  • 2014-12-30
  • 1970-01-01
  • 2014-09-16
  • 2016-05-26
  • 2018-07-08
  • 1970-01-01
相关资源
最近更新 更多