【问题标题】:AngularJS ng-click function not reachedAngularJS ng-click 功能未达到
【发布时间】:2015-11-24 00:23:45
【问题描述】:

当我将新产品添加到产品列表时,它不起作用。所以产品加载良好,但 ng-click 函数没有被调用。 (我在addProduct 函数中输入的警报未执行)。

HTML

<div ng-controller="ProductController as ProductCtrl">
Zoeken <input type="text" ng-model="search" placeholder="Search" />
<div>
    Filter Type
    <button ng-repeat="cat in categories" ng-click="filterProductsByCategory(cat)">{{cat}}</button>
</div>
<table cellpadding="5" cellspacing="0" border="1">
    <tr>
        <th>ID</th>
        <th>Product</th>
        <th>Type</th>
        <th>Price</th>
        <th>Toevoegen</th>
    </tr>
    <tr ng-repeat="product in ProductCtrl.products">
        <td>{{product.id}}</td>
        <td>{{product.name}}</td>
        <td>{{product.type}}</td>
        <td>{{product.price}}</td>
        <td></td>
    </tr>
    <tr><td></td>
        <td><input type="text" name="newProduct.name" ng-model="productCtrl.newProduct.name"></td>
        <td><input type="text" name="newProduct.price" ng-model="productCtrl.newProduct.price"></td>
        <td><input type="text" name="newProduct.type" ng-model="productCtrl.newProduct.type"></td>
        <td><a href ng-click="productCtrl.addProduct()">Product {{productCtrl.newProduct.name}} toevoegen</a></td></tr>
</table>

任何帮助将不胜感激。

控制器:

app.controller('ProductController', function(productService) {

    var that = this;

    productService.getProducts().success(function(data) {
        that.products = data;
    });

    this.newProduct = "";
    this.addProduct = function() {

        that.products.push(this.newProduct);
        window.alert(that.products);
        this.newProduct = "";

    };
});

【问题讨论】:

    标签: javascript angularjs angularjs-controller angularjs-ng-click angularjs-controlleras


    【解决方案1】:

    这是一个错字,你的控制器别名是ProductCtrl而不是productCtrl,另外你需要改变你的ng-model来纠正同样的事情

    productCtrl 替换为ProductCtrl 将解决您的问题。

    <tr>
      <td>
         <input type="text" name="newProduct.name" ng-model="ProductCtrl.newProduct.name"/>
      </td>
      <td>
        <input type="text" name="newProduct.price" ng-model="ProductCtrl.newProduct.price"/>
      </td>
      <td>
         <input type="text" name="newProduct.type" ng-model="ProductCtrl.newProduct.type"/>
      </td>
      <td>
        <a href ng-click="ProductCtrl.addProduct()">
           Product {{productCtrl.newProduct.name}} toevoegen
        </a>
      </td>
    </tr>
    

    【讨论】:

    • 谢谢,这行得通。有时您只是再也看不到它了,再次感谢!
    • @SanderNelen 发生这种情况..不要忘记单击向上箭头并接受答案..很高兴为您提供帮助..谢谢 :)
    猜你喜欢
    • 2015-01-26
    • 1970-01-01
    • 2016-10-28
    • 2023-03-31
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    相关资源
    最近更新 更多