Angular1发送变量到指令

时间:2017-02-24 07:59:55

标签: angularjs variables directive

如何将变量发送到指令?我的代码实际上没有用,但我从你的评论中做了一切:( 正如您在.html文件中看到的那样,{{ ctrl.emptyParent.name }}正在运行。

html的

<div cms-dropdown emptyParent="emptyParent.name" classes="btn-default">
    <i style="opacity: 0.8">{{ emptyParent.name }}</i>
</div>

指令

.directive('cmsDropdown', [function(){
    return {
        restrict: 'A',
        scope: {
            emptyParent: '=',
        },
        transclude: true,
        template:
            `
            {{emptyParent}} Hi

            `
        link: function($scope, $element, $attrs){

        }
    };
}]);

和变量

this.emptyParent = {
    _id: 'empty',
    name: '~~#(brak)#~~',
    parentAlbumId: null,
    position: -1,
    createdDate: undefined,
    modifiedDate: undefined
};

1 个答案:

答案 0 :(得分:1)

emptyParent="emptyParent.name"更改为empty-parent="emptyParent.name"

angular.module("app",[])
.controller("ctrl",function($scope){
	$scope.emptyParent = {"name":"jhon"}
})
.directive('cmsDropdown', [function(){
    return {
        restrict: 'A',
        scope: {
            emptyParent: '=',
        },
        transclude: true,
        template:
            `
            {{emptyParent}} Hi
              
            `,
        link: function($scope, $element, $attrs){

        }
    };
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<div cms-dropdown empty-parent="emptyParent.name" classes="btn-default">
    <i style="opacity: 0.8">{{ emptyParent.name }}</i>
</div>
</div>