- Install Dart SDK: Make sure you have the Dart SDK installed. You can download it from Dart’s official site.
- Install AngularDart: Create a new Dart project using the Dart command-line tools. Open your terminal and run:
dart create -t web-angular my_angular_app cd my_angular_app
- Add AngularDart Dependency: Open the
pubspec.yaml
file and add AngularDart dependencies:yamlCopy codedependencies: angular: ^6.0.0
Then run:
dart pub get
Step 2: Create a Simple Component
- Create a Component: In the
lib
directory, create a new file calledapp_component.dart
.
import 'package:angular/angular.dart'; @Component( selector: 'my-app', template: ''' <h1>Hello, AngularDart!</h1> <input [(ngModel)]="name" placeholder="Enter your name"> <p>Your name is: {{ name }}</p> ''', directives: [coreDirectives], ) class AppComponent { String name = ''; }
- Update the Main Entry Point: In
lib/main.dart
, update the code to bootstrap the AngularDart app.
import 'package:angular/angular.dart'; import 'package:my_angular_app/app_component.dart'; void main() { bootstrap(AppComponent); }
Step 3: Run Your Application
- Build and Serve the Application: You can run your application using:
webdev serve
- Open in a Browser: Navigate to
http://localhost:8080
in your browser. You should see the header and the input field.
Step 4: Add Styles
You can add styles to your application. Create a styles.css
file in the web
directory:
h1 {
color: blue;
}
input {
margin: 10px;
}
Then link it in web/index.html
:
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
Leave a Reply