Search code examples
dartangular-dartdart-html

Ng-click error (Angular dart)


Im new with this language and this framework, Ive made a command-line app and it works perfectly and now I wanna make a very simple web interface

I have this in my .html

<body ng-app>
.
.
.
<input type="submit" ng-click="doSomething()">

In my .dart

import 'dart:html';
import 'package:angular/angular.dart';
import 'package:angular/application_factory.dart';
.
.
.
void doSomething(){
lotToDo;
}

And I get this

Property 'doSomething' is not of type function.

Whats the point of the error ? Its necesary to use a controller ?

Thanks to all !


Solution

  • You need a controller that contains the doSomething() method.

    See https://github.com/angular/angular.dart.tutorial/blob/master/Chapter_03/lib/recipe_book.dart for example.

    The index.html has a tag <body recipe-book> tag. Angular applies the controller declared in recipe_book.dart here because this controller has the selector selector: '[recipe-book]' assigned which is a tag that has the attribute recipe-book.

    You also need to initialize a module so Angular knows of this controller.

    library recipe_book;
    
    import 'package:angular/angular.dart';
    import 'package:angular/application_factory.dart';
    
    import 'package:angular_dart_demo/rating/rating_component.dart';
    import 'package:angular_dart_demo/recipe_book.dart';
    
    class MyAppModule extends Module {
      MyAppModule() {
        type(RecipeBookController); // register controller in a module
        type(RatingComponent);      // register some other component
      }
    }
    
    void main() {
      applicationFactory()
          .addModule(new MyAppModule()) // tell angular to use the modul declared above
          .run();
    }
    

    My advice: work through this tutorial before trying random things https://angulardart.org/tutorial/