从指令执行函数

时间:2015-11-25 15:06:53

标签: angularjs angularjs-directive

如何从指令执行函数?

或其他变体可以做得更好。

页:

angular.module('app', ['bsTable'])
    .controller('MainCtrl', ['$scope', function ($scope) {
        $scope.refresh = function() {
            ???
        };
}]);

主控制器:

angular.module('bsTable', [])
    .directive('bsTable', [function () {
        return {
            restrict: 'EA',
            template: '<table></table>',
            replace: true,
            scope: {
                ???
            },
            link: function ($scope, $element) {
                $element.bootstrapTable();
            },                
            controller: function ($scope, $element) {
                $scope.refresh = function() {
                    $element.bootstrapTable('refresh');
                };
            }                
        }
    }]);

指令控制器中添加的功能:

@SpringBootApplication
@EnableOAuth2Sso
@RestController
public class SocialApplication extends WebSecurityConfigurerAdapter {

  @RequestMapping("/user")
  public Principal user(Principal principal) {
    return principal;
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.antMatcher("/**")
        .authorizeRequests()
        .antMatchers("/", "/login**", "/webjars/**")
        .permitAll()
        .anyRequest()
        .authenticated();
  }

  public static void main(String[] args) {
    SpringApplication.run(SocialApplication.class, args);
  }

}

1 个答案:

答案 0 :(得分:1)

在主控制器中:

$scope.functionToPass = function(anyParameter){ ... };

指令:

...,
scope: {yourFunction:'=yourFunction'},
...,

在HTML中:

<bs-table yourFunction="functionToPass">

现在您可以访问指令控制器中的功能并链接为$scope.yourFunction