Angularjs 从内部过滤服务
我想使用一个指令来显示一组数据。为此,我制作了这样一个元素:Angularjs 从内部过滤服务,angularjs,Angularjs,我想使用一个指令来显示一组数据。为此,我制作了这样一个元素:;属性类型决定了作业用户应该看到的作业类型,要在我的模板中进行筛选,我有以下代码:{{job.title} 我遇到的问题是,我不知道如何从我的元素中获取属性值,并将其与服务结果一起推送,而不会引起我的愤怒 我的指示: angular.module('myApp').directive('jobs', jobsDirective); jobsDirective.$inject = ['jobService','$scope','$sce
;属性类型决定了作业用户应该看到的作业类型,要在我的模板中进行筛选,我有以下代码:{{job.title}
我遇到的问题是,我不知道如何从我的元素中获取属性值,并将其与服务结果一起推送,而不会引起我的愤怒
我的指示:
angular.module('myApp').directive('jobs', jobsDirective);
jobsDirective.$inject = ['jobService','$scope','$sce'];
function jobsDirective(jobService, $scope, $sce) {
//this return results
var jobs = jobService.getAllJobs().success(function (data, status, headers, config) {
return data;
});
return {
restrict:'E',
replace:true,
templateUrl:'templates/jobs.html',
scope: {
type:'@'
},
link: function(scope){
scope.jobs = jobs;
}
}
}
这是工作代码;我刚刚为指令添加了一个控制器
"use strict";
/*
e.g. <jobs type="1" />
*/
angular.module('myApp').directive('jobs', jobsDirective);
angular.module('myApp').controller("jobsCtrl" ,jobsCtrl);
jobsCtrl.$inject = ['$scope', 'jobService'];
function jobsCtrl($scope, jobService, $sce) {
jobService.getAllJobs().success(function (data, status, headers, config) {
$scope.jobs = data;
});
}
function jobsDirective() {
return {
restrict:'E',
replace:true,
controller: 'jobsCtrl',
templateUrl:'templates/jobs.html',
scope: {
type:'@'
}
}
}
“严格使用”;
/*
例如
*/
角度.module('myApp')。指令('jobs',jobsDirective);
角度。模块('myApp')。控制器(“jobsCtrl”,jobsCtrl);
jobsCtrl.$inject=['$scope','jobService'];
函数jobsCtrl($scope,jobService,$sce){
jobService.getAllJobs().success(函数(数据、状态、标题、配置){
$scope.jobs=数据;
});
}
函数jobsDirective(){
返回{
限制:'E',
替换:正确,
控制器:“jobsCtrl”,
templateUrl:'templates/jobs.html',
范围:{
类型:“@”
}
}
}
这里是一个使用控制器获取type属性的代码。
请注意,在指令定义中使用bindToController
属性,可以通过作用域服务和controllerAs
别名访问type属性,在本例中称为ctrl
angular
.module('myApp')
.directive('jobs', jobsDirective);
function jobsDirective() {
return {
restrict:'E',
replace:true,
controller: jobsCtrl,
controllerAs: 'ctrl',
bindToController: true,
templateUrl: 'templates/jobs.html',
scope: {
type:'@'
}
}
}
jobsCtrl.$inject = ['$scope', 'jobService'];
function jobsCtrl($scope, jobService, $sce) {
var vm = this,
type = $scope.ctrl.type;
//USE THE TYPE ATTRIBUTE TO GET THE JOBS ACCORDING THE TYPE
jobService.getAllJobs(type).success(function (data, status, headers, config) {
vm.jobs = data;
});
}
尝试为您的指令创建一个控制器函数,然后在控制器中注入作用域服务,您将可以访问“type”属性。基于此值在控制器函数内调用您的服务我可以为指令创建控制器吗?在Google Scope中找不到它的引用。jobs是HTTP响应的承诺。你需要一系列的工作。当问一个让“angular愤怒”的问题时,发布完整的错误信息,而不是让我们猜测;我不知道我们可以在指令中添加控制器。谢谢你们教我在指令中使用控制器以及bindToController&controllerAs。我迄今为止最好的圣诞礼物哈哈