AngularJS服务未被调用

时间:2015-08-06 16:23:59

标签: javascript jquery angularjs angularjs-controller angularjs-factory

在这个小提琴中,我试图通过将文本传递给服务来格式化文本但是没有调用该服务:

http://jsfiddle.net/7MhLd/1038/

代码:

<div ng-controller="MyCtrl">
  <div ng-repeat="line in lines">
      <div class="preview">{{parseText(line.text)}}</div>
   </div>
</div>

var myApp = angular.module('myApp', []);

myApp.factory('parseText', function(data) {
    return "textParsed"
});

function MyCtrl($scope) {
    $scope.lines = [{
        text: 'res1'},
    {
        text: 'res2'}];
}

我正在返回textParsed以显示未调用服务 - div中没有​​显示任何文本。如何调用此服务&amp;传递参数line.text

1 个答案:

答案 0 :(得分:2)

在创建应用时,您犯了一些错误。

  • 在主要div上声明class Program { static void Main(string[] args) { using (MyContext ctx = new MyContext()) { City c = new City() { Id2 = 32767 }; ctx.Set<City>().AddOrUpdate(c); Person p = new Person() { Id2 = 32767 }; p.City = c; ctx.Set<Person>().AddOrUpdate(p); ctx.SaveChanges(); } } } class City { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public int Id2 { get; set; } } class Person { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public int Id2 { get; set; } public int CityId { get; set; } public int CityId2 { get; set; } public City City { get; set; } } internal sealed class CityConfiguration : EntityTypeConfiguration<City> { public CityConfiguration() { HasKey(t => new { t.Id, t.Id2 }); } } internal sealed class PersonConfiguration : EntityTypeConfiguration<Person> { public PersonConfiguration() { HasKey(t => new { t.Id, t.Id2 }); } } class MyContext : DbContext { public MyContext() { Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyContext>()); } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new CityConfiguration()); modelBuilder.Configurations.Add(new PersonConfiguration()); } }
  • 在您的应用内注入工厂相关性。
  • 从工厂返回对象,可以使用像parse这样的多种方法
  • 添加控制器时使用模块化方法,通过创建模块来附加角度组件。

<强>标记

ng-app="myApp"

<强>代码

<div ng-app="myApp" ng-controller="MyCtrl">
    <div ng-repeat="line in lines">
        <div class="preview">{{parseText.parse(line.text)}}</div>
    </div>
</div>

Working Fiddle