【发布时间】:2018-06-22 01:57:13
【问题描述】:
我目前在 AngularJS (https://next.plnkr.co/edit/qpyvZuvI8vw8q6on?open=lib%2Fscript.js&preview) 中定义了一个简单的搜索功能,但我希望将此功能迁移到现有的 Angular 应用程序中。我创建了一个新的 Angular 应用程序并将视图移动到 app.component.html
<head>
<script src="./script.js"></script>
</head>
<h1> Search Feature </h1>
<body ng-app="search" ng-cloak>
<div id="content" ng-controller="MainCtrl">
<input type='text' ng-model='searchText' placeholder=" Enter Query Here " />
<ul>
<li class="angular-with-newlines" ng-repeat="course in courses | filter:searchText">
{{course.course_number}}: {{course.title}}
<button ng-click="course.showDesc=!course.showDesc">See More</button>
<div ng-if="course.showDesc"> Description: {{course.description}} </div>
</li>
</ul>
</div>
</body>
然后我将控制器代码移动到一个名为script.js的javascript文件中
import angular from 'angular';
angular.module('search', []).controller('MainCtrl', function($scope) {
$scope.courses = [
{
id: 1,
course_number: '54 Mathematics',
title: 'Linear Algebra and Differential Equations',
description: 'Basic linear algebra; matrix arithmetic and determinants. Vector spaces; inner product as spaces.',
keywords: 'determinants, linear, equations, inner, basic, spaces, partial, order'
},
{
id: 2,
course_number: '110 Mathematics',
title: 'Linear Algebra',
description: "Matrices, vector spaces, linear transformations, inner products, determinants.",
keywords: "determinants, forms, products, eigenvectors, linear"
},
{
id: 3,
course_number: '89A Statistics',
title: 'Linear Algebra for Data Science',
description: 'An introduction to linear algebra for data science.',
keywords: 'ranking, prob, network, document, algebra, basics, model, matrices,'
}
];
});
但是,我无法访问控制器中定义的任何数据,并且应用程序无法运行。我对 Web 开发比较陌生,所以这不起作用,因为我需要将我的 javascript 代码转换为 typescript 吗?还是我需要以不同的方式导入我的代码?
感谢任何输入!谢谢!
【问题讨论】:
-
如果您是 Angular 新手,您应该知道 AngularJS (version=2) 只是名称相似。没有一种语法是兼容的。当时很多批评是他们应该使用一个新名称,而不是试图利用 AngularJS 的流行度来搭便车。但我想这对他们有用。 HTML 模板有些相似,如果您搜索“如何在 Angular 2 中使用
ng-something”之类的内容,通常会找到等价的模板。我猜那个过滤器不好。声明模块、控制器和注入范围都是不同的。 -
两者有很大不同。您可能必须重写所有内容......即使 Angular 与它的 alpha 版本相比也发生了很大变化,而 1.5 年前,无论是否使用 Angular,开发人员都非常沮丧,因为每次更新都会发生如此迅速的变化。但是感谢上帝,当前的框架非常稳定。新事物总是有可能出现,但这不会影响已经编写的代码,因为 Angular 团队还担心向后兼容性。
-
是的,即使您看到“angular 2, 4, 5, 6”,也只是因为semver,它们在大多数情况下几乎相同并且向后兼容。只是 1.x 是完全不同的野兽。
标签: javascript angularjs angular typescript angular-controller