Angularjs控制器指令



如何从指令中调用函数?当调用GetAllActivityActionDetails方法时,函数不会被激发。控制器功能:

 function GetAllActivityActionDetails() {
        alert("GetAllActivityActionDetails");
}

指令为:

app.directive("bwUpload", ['$log', '$http',  function ($log, $http) {
    return {
        scope: { project: "=", activity: "=", action: "=", document: "=" },
        link: function (scope, element, attributes) {
            element.bind("change", function (changeEvent) {
                var reader = new FileReader();
                reader.onload = function (loadEvent) {
                    $log.log('onload.byteLength: ' + loadEvent.target.result.byteLength);                    
                    scope.$apply(function () {                        
                        var p = 'project_id=' + scope.project._id + '&activity_id=' + scope.activity._id +
                              '&action_name=' + scope.action.name + '&document_id=' + scope.document._id
                        var config = {
                            url: 'baf/DocumentUpload?' + p,
                            method: 'POST',
                            headers: { 'Content-Type': 'application/octet-stream' },
                            data: new Uint8Array(loadEvent.target.result),
                            transformRequest: []
                        };
                        $http(config).then(
                          function (resp) {
                              var response = resp.data;
                              $log.log("DocumentUpload response (fileName, length): " + response.fileName + ", " + response.length);
                              alert('Document Uploded.');
                              //bwGlobals.refreshView()
                              alert(scope.activity._id);
                              //GetAllActivityActionDetails(scope.activity._id);
                              scope.$apply("GetAllActivityActionDetails()");
                              alert("GetAllActivityActionDetails");
                          },
                          function (errResponse) { alert(errResponse); }
                        );
                    });
                }
                reader.readAsArrayBuffer(changeEvent.target.files[0]);
            });
        }
    }
}]);

我想调用GetAllActivityActionDetails()表单指令,但它不起作用

虽然这不是一个很好的角度模式实践,但您可以在全局中创建函数,并使用窗口从指令调用。

var GetAllActivityActionDetails = function () {
  alert("GetAllActivityActionDetails");
}

然后在指令中触发它,就像:

window.GetAllActivityActionDetails();

相关内容

  • 没有找到相关文章

最新更新