
[英]get attribute object without using isolated scope angularjs custom directive
[英]Define custom directive with isolated scope depanding object value
当我循环访问它们时,我的控制器中有人员类型的对象列表,它可以正常工作并从列表中显示所有人员的姓名。 我已经定义了一个控制器来以结构化方式显示数据,但是为什么不允许将自定义指令的范围绑定到人员姓名的值,这件作品很好
<div ng-app="myapp">
<div ng-controller="MyController">
<div ng-repeat="per in person">
<!-- <test user=" -->
{{per.firstName}}
<!-- "></test> -->
</div>
但是当我尝试将自定义指令的范围绑定到对象的名字时,AngularJS抛出错误
错误:[$ parse:syntax]语法错误:从[{per.firstName}}]开始的表达式[{{per.firstName}}]第2列的标记'{'无效键。
<div ng-repeat="per in person">
<test user=" {{per.firstName}}"></test>
</div>
</div>
</div>
AngularJs cond:
var myapp=angular.module('myapp',[]);
myapp.controller('MyController',function($scope){
$scope.person=[
{firstName:"ali1",
lastName:"ahmad"
},
{firstName:"ali2",
lastName:"ahmad3"
},
{firstName:"ali4",
lastName:"ahmad"
},
{firstName:"ali5",
lastName:"ahmad"
},
];
});
myapp.directive('test',function(){
var directive={}
directive.restrict="E";
directive.template="Name : {{user.firstName}}";
directive.scope={
user:"=user"
}
return directive;
});
如何将自定义指令与对象指令的值绑定?
当您绑定到范围时,只需传递对象,不需要{{}},因此
user="per.firstName"
更改此行
<test user=" {{per.firstName}}"></test>
至
<test user="person"></test>
并在指令内使用ng-repeat。 首先像这样将数据从控制器发送到指令
<test user="person"></test>// Here you are sending whole array
然后收到您的user
这是一个包含的refrence person
那么你的指令内收到user
在某些变量,你做了指令这里面范围user: "=user"
尝试这个:
<test user="per"></test>
HTML:
myapp.directive('test', function () {
return {
restrict: 'E',
scope: {
user: '='
},
template: 'Name : {{user.firstName}}'
};
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.