【发布时间】:2020-01-18 08:36:21
【问题描述】:
我是 Angular 新手,我正在学习绑定表达式的基础知识。在尝试实现新模块时,我的 html 文件中不断出现错误,提示未定义标识符。我该如何摆脱这个错误?
"标识符'message'未定义。组件声明、模板变量声明、元素引用不包含这样的memberAngular "
我正在使用 Visual Studio Code 1.38.1 和 Angular 8.2.6;我已经尝试将脚本引用放在头部和身体的不同部分;尝试更改模块的名称,使用单引号和双引号,并修改控制器实例。消除错误的一件事是将属性放在 app.component.ts 文件中;但是,我仍然希望它成为模块,因为没有其他人遇到类似的问题。我不明白我做错了什么,而且我看到的任何教程都没有解决这个问题。
在 script.js 文件中:
<reference path="angular.min.js" />
var myApp = angular.module("module1", []);
myApp.controller("myController", function($scope){
$scope.message = "hello from the controller";
});
在 app.component.html 文件中:
...
<body>
<div ng-controller="module1">
<p>{{ message }}</p>
</div>
</body>
...
在 app.component.ts 文件中:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'proj1';
message = 'hello';
}
【问题讨论】:
-
您正在混合使用 AngularJs 和 Angular (2+)。
-
您正在将 angularjs 与 angular 混合使用。这些是完全不同的东西!请阅读文档以对 Angular 有一个基本的了解。 angular.io/tutorial
-
谢谢你们的澄清,我没有意识到他们是不同的。
标签: javascript angular node-modules