如何将范围值传递给指令控制器

时间:2015-05-11 07:11:16

标签: javascript angularjs

Hai我正在尝试将范围值数组从控制器传递给指令,以便它可能如何。

我需要将此值传递给我的指令控制器$scope.recipientsOrgIdArr.push(data.relatedEntityInstanceId);

HTML文件

<search  searchobj="tei_org"  selecteditemslist="recipientsOrgIdArr"   searchid="organisation" />

搜索指令

.directive('search', function ($timeout) {
    return {
        restrict: 'AEC',
        scope: {
            selecteditemslist: "="
}
link: function (scope, elem, attrs) {
console.log(scope.selecteditemslist);  // getting as undefined

//how can i get scope value here
}
}
})

请提前帮助我解决此问题

2 个答案:

答案 0 :(得分:1)

您遇到的问题是,在调用指令链接函数时尚未设置selecteditemslist

试试这个

.directive('search', function ($timeout) {
    return {
        restrict: 'AEC',
        scope: {
            selecteditemslist: "="
        }
        link: function (scope, elem, attrs) {
            scope.$watch('selecteditemslist', function(value) {
                if(value){
                    console.log(scope.selecteditemslist);
                }
        }...

答案 1 :(得分:0)

尝试使用此代码:

    .directive('search', function ($timeout) {
        return {
            restrict: 'AEC',
            scope: {
                selecteditemslist: "="
    }
    link: function (scope, elem, attrs) {
    attrs.$observe('selecteditemslist', function(value) {
    console.log(value);
  });
    }
    }
    })