【问题标题】:'ListCtrl' is not a function, got undefined'ListCtrl' 不是函数,未定义
【发布时间】:2015-11-06 09:53:16
【问题描述】:

不太清楚这是怎么回事,但我有一些东西在 Monaca 之外运行得很好,一旦在 Monaca 我得到了这个: 错误:[ng:areq] 参数 'ListCtrl' 不是函数,未定义

代码如下:

<script>
var app = ons.bootstrap('propertyDeal', ['onsen']);
app.factory('Property', function () {

    /**
    * Constructor, with class name
    */
    function Property(newProperty) {
        // Public properties, assigned to the instance ('this')
        this.purchasePrice = newProperty.pprice;
        this.marketValue = newProperty.mv;
        this.stampDuty = newProperty.stamp;
        this.sourcingFee = newProperty.sourcing;
        this.legaFee = newProperty.legal;
        this.otherFee = newProperty.other;
      }

    /**
    * Public method, assigned to prototype
    */
    Property.prototype.getPurchasePrice = function () {
        return this.purchasePrice;
    };

    return {
        createNew: function(newProperty) {
            console.log( "Alert accomplished" );
            return new Property(newProperty);
        }
    };
});

app.factory('portfolio', function(Property){
    var portfolio = {};
    portfolio.list = [];

    portfolio.add = function(newProperty){
        var prop = Property.createNew(newProperty);
        portfolio.list.push(prop);
        alert(prop.purchasePrice);
    };

    return portfolio;
}); 

app.controller('ListCtrl', function(portfolio) {
    var self = this;
    self.portfolio = portfolio.list;
});

app.controller('PostCtrl', function(portfolio){
    var self = this;

    self.addProperty = function(newProperty){
        portfolio.add(newProperty);
    }; 
});

</script>

<ons-navigator>
<ons-page>
    <div class="container" ng-controller="TopMenuController" ng-init="init('Initial investment','ion-social-usd')">
        <div ng-include="url"></div>
    </div>

    <div style="text-align: center">
        <h2>Initial investment</h2>

        <ul class="list">
                <input type="hidden" ng-model="newProperty.id" placeholder="id">
            <li class="list__item">
                <input type="text" class="text-input text-input--transparent" style="width:100%; margin-top:4px;" ng-model="newProperty.pprice" placeholder="Purchase price">
            </li>
            <li class="list__item">
                <input type="text" class="text-input text-input--transparent" style="width:100%; margin-top:4px;" ng-model="newProperty.mv" placeholder="Market value">
            </li>
            <li class="list__item">
                <input type="text" class="text-input text-input--transparent" style="width:100%; margin-top:4px;" ng-model="newProperty.stamp" placeholder="Stamp duty">
            </li>
            <li class="list__item">
                <input type="text" class="text-input text-input--transparent" style="width:100%; margin-top:4px;" ng-model="newProperty.sourcing" placeholder="Sourcing fee">
            </li>
            <li class="list__item">
                <input type="text" class="text-input text-input--transparent" style="width:100%; margin-top:4px;" ng-model="newProperty.legal" placeholder="Legal fee">
            </li>
            <li class="list__item">
                <input type="text" class="text-input text-input--transparent" style="width:100%; margin-top:4px;" ng-model="newProperty.other" placeholder="Other">
            </li>
        </ul>
    </div>
    <br />
    <div data-ng-controller="ListCtrl as list">
        <p ng-repeat="prop in list.portfolio track by $index">
            New Object: {{prop.purchasePrice}}: {{prop.legaFee}} </p>
    </div>

    <div ng-controller="PostCtrl as post">
        <input type="button" ng-click="post.addProperty(newProperty)" value="Create Property"></input>
        <p>{{newProperty}}</p>
    </div>
 </ons-page>
</ons-navigator>

我不明白这有什么问题,例如在小提琴中测试时知道完全相同的代码正在工作 http://jsfiddle.net/XmWUP/129/

感谢您的帮助

【问题讨论】:

  • 你在哪里定义你的应用程序?没有var app = angular.module('name', []); 它有空数组吗?如果它丢失并且您尚未定义它,则可能会导致此错误。该数组告诉角度“创建这个模块”,否则它说“找到我这个模块”。也很抱歉,这是对宠物的厌恶,但您的代码中无需将 this 别名为 self
  • 感谢 ste2425。 bootstrap 在另一个 pahe 上,我已经添加了它,这与 onsen-ui 有点不同,但如果我理解得很好,它与模块声明的作用相同。我知道 self 和 this ,但是之前编辑了一些代码,使它变得不必要并且没有更改它,但是正如你所说,这不是这里的问题。

标签: angularjs onsen-ui monaca


【解决方案1】:

您的代码看起来不错,除了未定义的ng-controller="TopMenuController"

可能问题与 Angular 版本有关,您的 jsfiddle 示例使用 1.2.6,但 Monaca 使用 1.4.3Onsen UI 1.3.11

检查这个CodePen example

【讨论】:

  • 感谢您的回答。 TopMenuController 是在另一个页面中定义的,我只是复制了页面。
  • 我认为这是因为版本不同,但我不明白如何改变以及如何使其与此版本一起使用。除了在 1.3.11 上运行之外,我看不出您的 codepen 与我的代码有何不同
  • Onsen UI 1.x 基于 Angular,一些元素已从 Angular 1.2 到 1.4 被弃用/更改/添加,这可能就是原因。
猜你喜欢
  • 1970-01-01
  • 2014-06-04
  • 2016-10-04
  • 2015-06-27
  • 2018-05-14
  • 2015-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多