Angular'undefined不是一个函数'定义组件

时间:2016-11-11 19:17:01

标签: javascript angularjs

我有工作的离子代码允许用户输入一个列表,我试图将其分解为一个组件以供重复使用。但是在运行时,我在js的第4行得到错误undefined is not a function。如何解决这个问题?

/*jshint multistr: true */         
var app = angular.module('ionicApp', ['ionic']);

app.component('inputlist',{
    bindings: {label: '@'},
    template: '\
        <div class="list">\
            <div class="item item-button-right">\
                <ion-label floating>Enter {{$ctrl.label}} below then press +</ion-label>\
                <input type="text" ng-model="nameinput.name"></input>\
                <button class="button button-assertive" ng-click="addItem()">+</button>\
            </div>\
            <div class="item item-button-right" ng-repeat="item in items track by $index">\
                <ion-label>{{item.name}}</ion-label>\
                <button class="button button-positive" ng-click="items.splice($index,1)">-</button>\
            </div>\
        </div>',
    controller: function() {
        this.items = [];
        this.nameinput = {name:''};
        this.addItem = function(){
            var name = this.nameinput.name.trim();
            if (name!=="") 
            {
                this.items.push({name:name});
                this.nameinput={name:""};
            }
        };
    }    
});

HTML

<!DOCTYPE html>
<html ng-app="ionicApp">
<head>

  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">

  <title>Ionic-AngularJS-Cordova</title>

  <!-- Ionic framework - replace the CDN links with local files and build -->    
  <link href="lib/ionicframework/ionic.min.css" rel="stylesheet">
  <script src="lib/ionicframework/ionic.bundle.min.js"></script>

  <script src="js/app.js"></script>

  <script src="cordova.js"></script>

</head>


  <body>

    <ion-content>

        <inputlist label="foo"></inputlist>

    </ion-content>

</body>
</html>

1 个答案:

答案 0 :(得分:2)

你提到你在你的项目中使用了Ionic v1.0.0-beta1,我相信这个版本的离子带有1.3以下的角度版本。

另一方面,

组件是角1.5及以上的新功能。这解释了为什么在第4行app.component() method中未定义函数。

我建议你采用这两种方法中的任何一种来解决你的问题:
1)将组件转换为指令 2)将离子升级到v1.3.2(最新版本),支持角度1.5