通过bindToController将控制器范围绑定到指令作为引用不起作用

时间:2016-02-12 10:44:22

标签: angularjs

当我尝试将控制器作用域映射为指令它不起作用时,我是angular bindToController的新手。

Example AS: 
app.controller('test',function($scope){
       $scope.reftodir = $scope;
       $scope.test = 'someconte';
    })

 in html referenced it as :

     <test-directive testscope = 'reftodir' iso-scope="test"/>


app.directive('testDirective',function(){

      return{
         scope:{
             isoScope: '='
         },
         bindToController: {
            testscope:'='
         },
      }
    })


  Suppose i have a function in controller as test_function, i need to call that function from directive with the help controller scope available from bindToController variable such as testscope.test_function().

but test_function is not available. Please provide any suggestions, Thanks in advance friends.

1 个答案:

答案 0 :(得分:-1)

您的指令对象应如下所示:

Low (Medium)    Incomplete or No Cache-control and Pragma HTTP Header Set

Description

The cache-control and pragma HTTP header have not been set properly or are missing allowing the browser and proxies to cache content.

URL

<redacted url>

Evidence

public, must-revalidate, proxy-revalidate

然后在您的控制器中,您应该能够看到这样的范围:

return {

    controller: 'test',
    bindToController: true,
    controllerAs: 'vm'
    scope: {
       testScope: '='
    }   

}

请注意,如果我们绑定到控制器,我们将不再需要$ scope,而app.controller('test',function(){ this.testScope //scope is available here }) 部分指向控制器自己