【发布时间】: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>É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>É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>É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)