AngularJS 控制器 returns 表单

AngularJS controller returns form

开发者!

我决定尝试Angular,所以我想在MVC(或接近它)架构中制作简单的图片上传和编辑应用程序。

而且我停留在入口点上,因为我真的不知道如何从模板中正确 return 填充 main form

正如我现在所想 - 我先调用 controller 函数,然后它应该调用 mainService 函数,该函数构建表单,使用 'imageUploadController',使用 imageUploadService .

但我有件事告诉我,做那样的事情是不对的。

我的主要问题是如何在没有链接或包含的情况下获取 uploadImageForm.html、将其传递给 index.html(我只是想将它们分开) 确保那里的服务和控制器正常工作

相同情况下的最佳做法如何?

这是代码,希望它有助于更​​清楚地看到我遇到的问题:

这是我的 index.html

<!doctype html>
<html lang="en" ng-app="modernFilter">
  <head>
    <meta charset="utf-8">
    <title>Modern.Filters</title>
    <script src="asstes/libs/angular.min.js"></script>
    <script src="app/components/app.js"></script>
    <script src="app/shared/imageUploadController.js"></script>
  </head>
  <body ng-controller = "mainController">
    <span>{{mainForm}}</span>
  </body>
</html>

这里是app.js

'use strict';

// Define the `modern.filter` module
var modernFilterApp = angular.module('modernFilter', []);

//  Define main controller

modernFilterApp.controller('mainController', ['$scope', function($scope, mainService){
    // Stores form data
    $scope.mainForm = null;

    // Returns main form template
    $scope.getMainForm = function(){
        $scope.mainForm = mainService.getMainForm();
    }

    // Call function on documentready
    angular.element(document).ready(function () {
        $scope.getMainForm();

        //todo: is it even okay? but without it nothing happens
        $scope.$apply();
    })

}]);

// Define service

modernFilterApp.service('mainService', function(mainService){
    this.getMainForm = function(){
        // todo: how should I get template here?
        return "filled form template from here"
    }
});

我也有模板imageUploadView

<div ng-controller="imageUploadController">
<canvas id="imageCanvas"></canvas>
<form action="">
    <input my-upload type="file" name="upload" onchange="angular.element(this).scope().uploadImage()">
</form>

它的控制器和服务(有示例代码,还在调试中)

modernFilterApp.controller('imageUploadController', ['$scope', 'imageUploadService', function($scope, imageUploadService) {
  // Stores image
  $scope.image = null;

  // Returns main form template
  $scope.uploadImage = function() {
    $scope.image = imageUploadService.handleImage(arguments);
  }

}]);

//  Define upload service

modernFilterApp.service('imageUploadService', function(imageUploadService) {

// Function handles uploaded files (dirty code)
this.handleImage = function(event) {
  var canvas = angular.element('#imageCanvas'),
    context = canvas.getContext('2d'),
    reader = new FileReader(),
    img = new Image();

  canvas.width = img.width;

  canvas.height = img.height;
  context.drawImage(img, 0, 0);
  img.src = event.target.result;
  return reader.readAsDataURL(e.target.files[0]);
}

}]);

不要尝试从控制器获取模板。

您需要使用指令。 Make 指令,其控制器为 imageUploadController ,其模板为 imageUploadView 并将其包含在 index.html

你的 html :-

<body ... >
  <my-directive></my-directive>
</body>
//include directive file,controller files

指令:-

    .directive('myDirective', ['your service',function(your service) {
  return {
    restrict: 'E',
    transclude: true,
    scope: {},
    templateUrl: 'template url',
    link: function (scope) {

    },
    controller : function($scope){
    //your controller
    }
  };
}]);

Refer here

Html 将自动呈现以代替指令