[英]How match unique id from template to a filter function in controller?
Here is my app.js : 这是我的app.js :
.state('app.study_collections', {
url: "/studies/:studynodeRef",
views: {
'menuContent': {
templateUrl: "templates/rubyonic/overview.html"
}
}
})
Here is a snippet from my studies.html : 这是我的study.html的摘录:
<div class="feed-item">
<div class="feed-media" ng-repeat="study in studies">
<img src="{{study.image}}" class="feed-image">
<div class="feed-gradient-overlay"></div>
<a href="#/app/studies/{{study.nodeRef}}">
<h4 class="feed-title">{{study.title}}</h4>
</a>
</div>
</div>
Here is a snippet from my controller.js : 这是我controller.js的片段:
Studies.all().then(function(payload){
$scope.studies = payload;
});
Studies.all().then(function(studies){
$scope.study = $filter('filter')(studies, function (d) {return d.nodeRef === '"'+ $stateParams.studynodeRef + '"';})[0];
});
So Studies.all()
makes call to the studies service which makes asynchronous calls to a json file to get data. 因此Studies.all()
调用了Studies.all()
服务,该服务对json文件进行异步调用以获取数据。
Studies.all().then(function(payload){
$scope.studies = payload;
});
This gets the list of all the studies which are iterated in the studies.html file. 这将获得在Studies.html文件中迭代的所有研究的列表。 Each study has its unique nodeRef. 每个研究都有其唯一的nodeRef。 When you click on a study it maps to the url "#/app/studies/{{study.nodeRef}}"
. 当您单击研究时,它会映射到URL "#/app/studies/{{study.nodeRef}}"
。 After that I want to use that nodeRef to filter out a study object from the studies collection which is supposed to be done by 之后,我想使用该nodeRef从研究集合中过滤出一个研究对象,这应该由
Studies.all().then(function(studies){
$scope.study = $filter('filter')(studies, function (d) {return d.nodeRef === '"'+ $stateParams.studynodeRef + '"';})[0];
});
where d.nodeRef === '"'+ $stateParams.studynodeRef + '"'
matches it with the correct nodeRef and filters out that study. 其中d.nodeRef === '"'+ $stateParams.studynodeRef + '"'
将其与正确的nodeRef匹配,并过滤出该研究。 But when I try to simply print the study in the template like {{study}}
, nothing appears and when I do a console.log()
it is undefined. 但是,当我尝试简单地将模板中的研究打印为{{study}}
,什么也没有出现,而当我执行console.log()
它是不确定的。
How do I fix this? 我该如何解决? Please let me know if any clarifications are needed. 请让我知道是否需要任何澄清。
Edit 编辑
I made some changes to my controller. 我对控制器进行了一些更改。 I am able to access the nodeRef from the url now but it doesn't work in my filter function for some reason unless I manually enter it. 我现在可以从URL访问nodeRef,但是由于某种原因,除非我手动输入它,否则它在我的过滤器功能中不起作用。 Have a look at it here https://gist.github.com/shubhamsinha92/01b492c62c4dd5ad3a7c 在这里看看https://gist.github.com/shubhamsinha92/01b492c62c4dd5ad3a7c
This is what my services.js looks like: 这是我的services.js的样子:
var app = angular.module('starter.services', [])
.factory('Studies',function($http,$filter,$q){
var deferred = $q.defer();
var studies = [];
$http.get("studies.json").success(
function(data){
//studies = data;
angular.copy(data, studies);
//At this point you can filter data as required, or not at all. I would suggest returning the entire JSON response and not filtering here at all, let the controllers filter the data as needed to maintain a layer of separation between controller and service.
deferred.resolve(studies);
}
);
return {
all: function(){
return deferred.promise;
}
};
})
The appears to be that you are comparing '"nodeId"' to 'nodeId'. 看来您正在将“ nodeId”与“ nodeId”进行比较。
Original 原版的
$filter('filter')(payload, function (d) {
$scope.id = '"' + $rootScope.nodeId + '"';
return d.nodeRef === $scope.id ;})[0];
Solution 解
$filter('filter')(payload, function (d) {
$scope.id = "" + $rootScope.nodeId;
if (d.nodeRef === $scope.id)
return d;})[0];
Please see this example 请看这个例子
https://jsfiddle.net/HB7LU/15157/ https://jsfiddle.net/HB7LU/15157/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.