【问题标题】:New items don't displaing in select-box from array factory use AngularJS新项目不会在数组工厂使用 AngularJS 的选择框中显示
【发布时间】:2016-03-09 14:52:08
【问题描述】:

你好,我创建了简单的 AngularJS 应用程序,我有两个按钮(创建新手机创建新类别),我保存了我的类别进入数组使用工厂:

myApp.factory('categoriesStorage', function () {
    var storage = [];
    var categories = ["Apple","HTC"];

    storage.addCategory = function (categoryTest) {
        categories.push(categoryTest);
        console.log(categories);
    }

    storage.getCategories = function()
    {
        return categories;
    }

    return storage;
});

Add New Phone 表单我有 dx-select-box -element:

<div class="dx-label">Phone Name</div>
               <div dx-select-box="nameOfPhone" ng-model="phoneName"></div><br/><br />

为了从数组中获取元素,我使用 HomeController.js 中的下一个代码 //弹出的手机名称选择框

  $scope.nameOfPhone = {
        items: categoriesStorage.getCategories()

    };

有效

直到我改变焦点或关闭添加新类别表单时,新元素添加到数组中但不显示到选择框中。

感谢您的回答!

【问题讨论】:

标签: arrays angularjs factory devextreme


【解决方案1】:

当显示添加电话视图时,您需要再次呼叫该线路。

$scope.nameOfPhone = {
    items: categoriesStorage.getCategories()
};

【讨论】:

  • 我这样做了:$scope.nameOfPhone.items = phoneAndCategoriesStorages.getCategories(); - 没什么(可能你有其他想法吗?我的应用程序的源代码你可以找到github.com/Ubuntus/DevExtremeAngularJSChartsTest/blob/master/…
  • 无法找到控制器的正确视图。此外,除非您将数据存储在类别的控制器数组上而不是保存到服务中,否则没有其他解决方案,因为您拥有相同的控制器。因此,每当您更新类别时,控制器都会拥有最新的类别。如果以上解决方案解决了您的问题,请将其标记为答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多