是否可以使用angularjs中的方法显示数据而不使用范围变量

时间:2018-06-03 19:31:26

标签: javascript angularjs methods

我有一个方法:

<span ng-init="getJobApplicantsList(jobId)">(number should be display here)</span>

是否可以显示数据而不将其存储到范围变量

因为我在很多地方使用这种方法。

这是我的控制者:

var set = [blah,blah];//object
$scope.getJobApplicantsList = function(jobId) {
findByMatchingProperties(set, { jobId: jobId }).length; //i have a function and i'm getting number here Example:`250` int
}

我想要这样的事情:

 <span>{{getJobApplicantsList(jobId)}}</span> 

2 个答案:

答案 0 :(得分:2)

只需返回号码

$scope.getJobApplicantsList = function(jobId) {
   return   findByMatchingProperties(set, { jobId: jobId }).length;  
}

并在模板中调用该函数

<span>{{getJobApplicantsList(jobId)}}</span> 

答案 1 :(得分:1)

只需从函数返回值,无需将其存储到任何范围变量中。

您还可以使用 -

来避免使用$ scope

JS -

var vm = this;
vm.getJobApplicantsList = function (jobId) {
            return findByMatchingProperties(set, { jobId: jobId }).length;
        }

HTML -

<div ng=controller="AppController as vm">
  <span ng-bind="vm.getJobApplicantsList(jobId)"></span>
</div>