【问题标题】:Angular filtering data in javascript is not displaying results and push of data causes error plunker providedjavascript中的角度过滤数据不显示结果,并且数据推送导致提供错误plunker
【发布时间】:2016-08-31 07:00:57
【问题描述】:

好的,我的 Angular 网站设置方式似乎有太多问题,所以我把它放在一个 plunker 中,这样任何人都可以看到它。

原问题:Angular retrieve specific data into $scope variable is not working

Plunker http://plnkr.co/edit/NsE29zjraQp9UeklJBiI?p=preview

我的问题是 1. 我不明白如何使用 app.filter 2.应用名称问题 3. forEach 在 $http.get 内推入会抛出未定义的错误

plunker Index.html 有模板代码循环,app.module.js 是 root,而 device.controller.js 是我使用控制器的地方,我使用 json 文件调用 $http.get 来伪造它。

我试图使用其他人的答案,所以这个代码

$scope.devices = result.data.Devices;   // gives all data ... 

过滤器我想知道这是否有用

<div ng-repeat="device in devices">
    {{ device.DeviceStatus }} 
</div>

然后这段代码我不确定它在正确的“地方”

似乎我不理解“应用程序”

app.filter('deviceStatus', function () {
    return function (status_id) {
       var statuses = ['Old Device', 'New Device', 'Activated', 'Unactivated'];
       return statuses[status_id];
   };
});

过滤器示例:

<td>{{device.DeviceId | deviceStatus}}</td>

【问题讨论】:

  • 我无法理解您想要达到的目标。请提供一些解释。
  • 2 个链接提供了原始问题,在该问题中我被告知尝试 app.filter,因此在 plunker 中,您看到我将其链接在控制器 js 文件的顶部,并且我正在尝试通过 push 获取 foreach 循环工作并引发错误。

标签: javascript angularjs angularjs-directive angularjs-scope angularjs-ng-repeat


【解决方案1】:

让我试着理解你的问题。

根据您的问题,您似乎无法理解app 是什么以及如何使用filter

这是您的 plunkr 的工作版本。 Check this url

  1. app 在您的项目中是 ng-app 指令。 ng-app 指令告诉 AngularJS 该元素是 AngularJS 应用程序的“所有者”。
  2. 用于了解过滤器功能。检查下面的例子。
  3. 您试图推入尚未定义的$scope.statuses。所以首先将$scope.statuses定义为一个空数组,即`$scope.statuses = [];

希望这对你有用!`

// To declare a filter we pass in two parameters to app.filter

// The first parameter is the name of the filter 
// second is a function that will return another function that does the actual work of the filter

//here app is the module name of your project

app.filter('myFilter', function() {

  // In the return function, we must pass in a single parameter which will be the data we will work on.
  // We have the ability to support multiple other parameters that can be passed into the filter optionally
  return function(input, optional1, optional2) {

    var output;

    // Do filter work here

    return output;

  }

});

【讨论】:

  • 太棒了!快速提问 - 为什么有些人这样做 var app = angular.module('demoApp',['ngRoute', 'ngAnimate']); 而不是我如何这样做 angular.module('app', ['angularUtils.directives.dirPagination']);
  • 当你在你的项目中定义一个模块时,最好的做法是把它放在一个全局变量或任何这样的地方,这样你下次可以直接将模块分配给任何组件。例如:如果您的应用程序太小,angular.module('app', [dependencies]) 看起来不错。但是,当您使用大型且可扩展的应用程序时,最好使用 var app = angular.module('app', []); 这样的用法,以便我们可以开始编写代码的另一部分,例如 app.filter('filtername', function(){}).controller('controllerName', function(){}) 等,
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-28
  • 2021-08-22
  • 1970-01-01
  • 2017-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多