AngularJS = 作用域中的 = 作用域导致解析语法错误

AngularJS = scope in an = scope causing parse syntax error

本文关键字:作用域 语法 错误 AngularJS      更新时间:2023-09-26

我正在尝试使用 Angular 中的指令实现 2 级层次结构。第一个范围venture传递一个 JSON 创业数组。在每个企业中都有一系列职位。理想情况下,第二个范围拾取此位置对象并将其填充。

    app.directive('venture',function(){
    return {
        restrict: "E",
        template: '<div><div ng-show="!ventureshow">{{details.venture_name}}'
                +'<positions data="{{details.positions}}" ></positions>'
                +'...',
        scope: {
            details:'='
        },
        replace: true,
        link:function($scope, element, attrs){
        }
    }
});
app.directive('positions',function(){
    return {
        restrict: "E",
        template:'d<ul ng-repeat="position in details">{{position}}<li>{{position.position_name}}:{{position.position_description}}</li></ul>',
        scope:{
            data:'='
        }
    }
})

但是,第二个对象上的等号 (=) 会导致错误$parse:syntax 。似乎无法弄清楚导致问题的原因

双向绑定需要绑定到可以分配给的属性。相反,您通过执行无法分配给data="{{details.positions}}"来提供插值(除非您在作用域上具有与插值结果相同的属性)。您必须收到不可分配的异常。执行双向绑定时,从绑定中删除{{插值。

template: '<div><div ng-show="!ventureshow">{{details.venture_name}}'
            +'<positions data="details.positions" ></positions>'

如果你使用2种方式绑定方式(=),你必须传递给他变量,而不是变量的值,因为和gular指令需要一个变量(place)来改变一些东西,写一些东西。