将$ scope用于服务 [英] Using $scope into a service

查看:91
本文介绍了将$ scope用于服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下代码的期望是它会绑定$ scope.as。但没有显示任何内容,控制台中没有显示错误

What I expect from below code is it would bind $scope.as . But nothing is displaying and no error is shown in console

var testModule = angular.module('testmodule', [])
.controller('ctrl', function ($scope, mser) {
  mser.aa();
 })
.service('mser', function($scope /* Would $scope be here */) {
  this.aa = function(){
  $scope.as = "hello mars"
 }});


推荐答案

你不能在服务中使用$ scope你应该在你的控制器中使用它,它用于粘合控制器和DOM。由于服务是在控制器中注入的,因此没有单独的服务存在。

you can't use $scope in service you should use it in your controller it is used to glue controller and DOM. Since services are injected in controllers so there is no individual existence of a service.

这篇关于将$ scope用于服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆