【发布时间】:2017-07-05 10:45:16
【问题描述】:
我有一个服务:
@Injectable()
export class MyService implements IMyService {
myServiceArray: Array<string> = ["hi", "hello", "yoyo"];
}
这个服务被注入到一个用 ngModel 更新字符串数组的组件中。当我尝试从组件或服务打印数组时,一切正常(又名数组使用 ngModel 更新)。
我也在另一个 1 中 @Inject 这样的服务。
@Injectable()
export class AnotherService implements IAnotherService {
constructor(public myService: MyService) {
}
printValues() {
console.log(this.myService.myServiceArray);
}
}
当我调用 printValues() 时,即使我使用模型更新了数组的值,也会打印 ["hi", "hello", "yoyo"]!
我做错了什么?
编辑:
组件的代码如下。
@Component({
selector: 'app-root',
providers: [MyService],
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppCustomerData implements IAppCustomerData {
constructor(public myService: MyService) {
}
}
【问题讨论】:
-
你们在哪里提供服务(
@Component()或@NgModel())? -
我放了组件的代码。 [(ngModel)]='...' 在组件的 templateUrl 中。
标签: javascript angular typescript angularjs-scope