相关疑难解决方法(0)

$ scope.$ root和$ rootScope有什么区别?

我在控制器中看到$ scope有$ root,这是什么?它与$ rootScope的不同之处在于可以在控制器中注入?

javascript angularjs angularjs-scope

42
推荐指数
2
解决办法
4万
查看次数

角度 2 中根范围的替代方案是什么?

我希望子组件访问共享服务并在将子组件注入主组件之后获取数据。我想让sharedservice(rootscope)的数据直接把数据放到mainComponents HTML中,就像这里

主组件.ts

import { Component } from '@angular/core';
import {ChildComponent} from './child';
import {AppServiceService} from './app-service.service';

@Component({
  moduleId: module.id,

  selector: 'rootscope-app',

  templateUrl: 'rootscope.component.html',

  styleUrls: ['rootscope.component.css'],

  directives:[ChildComponent]

})

export class RootscopeAppComponent {

  title = 'rootscope works!';

  display:any;

  constructor(appServiceService:AppServiceService){  

    this.display=appServiceService.getter();

  }
}
Run Code Online (Sandbox Code Playgroud)

共享服务.ts

import { Injectable} from '@angular/core';


@Injectable()

export class AppServiceService {

  ser = "hello i am from service";

  public data: any;

  constructor() {

  }

  settter(data: any) {

    this.data = data;

  }

  getter() {

    return this.data; …
Run Code Online (Sandbox Code Playgroud)

angular

6
推荐指数
1
解决办法
2万
查看次数

标签 统计

angular ×1

angularjs ×1

angularjs-scope ×1

javascript ×1