请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

全局变量注入失败报错 $injector:unpr。。。

如题。。


dict.js:

'use strict';

angular.module('app').value('dict', {}).run(['$http', function($http){
  $http.get('data/city.json', {})
    .then(function(resp){
      dict.city = resp.data;
    }).catch()
  $http.get('data/salary.json', {})
    .then(function(resp){
      dict.salary = resp.data;
    }).catch()
  $http.get('data/scale.json', {})
    .then(function(resp){
      dict.scale = resp.data;
    }).catch()

}])


正在回答 回答被采纳积分+3

3回答

提问者 weibo_我看你是饿得慌_03361000 2017-03-28 20:52:56
'use strict';

angular.module('app').service('dict', function($http,$q){
  this.getList = function(index){
    var def = $q.defer();
    $http.get('data/'+index+'.json', {})
    .then(function(resp){
      def.resolve(resp);
    })

    return def.promise;
  }
})


1 回复 有任何疑惑可以回复我~
  • 同学请教个问题,1.6.3的版本value创建全局变量了失效,dict注册成一个自定义服务,怎么用dict来取数据啊?谢谢
    回复 有任何疑惑可以回复我~ 2017-05-07 21:14:53
提问者 weibo_我看你是饿得慌_03361000 2017-03-28 18:22:51

解决了,1.6.3的版本好像不能用value创建全局变量了,我把dict注册成一个自定义服务了,然后可以正常使用了。



1 回复 有任何疑惑可以回复我~
  • sdddw23 #1
    怎么弄的呀
    回复 有任何疑惑可以回复我~ 2017-03-28 19:07:01
沙漠江河 2017-04-26 22:38:36


你好!同学我遇到的问题和你差不多,你是怎么解决的,我用的1.6.4版本,angular.module('app').value('dict', {}).run(['dict',使用value定义全局变量时报错

0 回复 有任何疑惑可以回复我~
  • 我是把dict注册成自定义服务了,你可以看一下我下边的代码
    回复 有任何疑惑可以回复我~ 2017-05-11 20:05:32
  • angular.module("app").run(["$http","$rootScope",function($http,$rootScope){
    	$rootScope.dict = {};
    	$http({
    		url:"data/city.json",
    		method:'get'
    	}).then(function successCallback(response){		
    		$rootScope.dict.city = response.data;
    	});
    	
    	$http({
    		url:"data/salary.json",
    		method:"get"
    	}).then(function successCallback(response){
    		$rootScope.dict.salary = response.data;
    	});
    	
    	$http({
    		url:"data/scale.json",
    		method:"get"
    	}).then(function successCallback(response){
    		$rootScope.dict.scale = response.data;
    	});
    }])
    回复 有任何疑惑可以回复我~ 2017-11-29 17:18:17
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信