Access an attribute from another module

Asked

Viewed 680 times

0

I need to update the values within the message directive through the keep-vendor-form directive, both are within the keep-vendor directive. Is it possible ? If so, how do I do it ?

maintain-provider.html

<cabecalho-interno entidade="Funcionario" acao="Pesquisar"></cabecalho-interno>

<section class="content">
    <mensagem error="msgError" sucesso="msgSucesso" info="msgInfo"></mensagem>
    <div class="col-md-12">
        <div class="box box-primary">
            <div class="box-header">
                <h3 class="box-title">Casdastro</h3>
                <div class="box-tools">
                    <button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
                </div>
            </div>
            <manter-fornecedor-formulario></manter-fornecedor-formulario>
        </div>
    </div>
    <hr />
</section>

maintain-supplier.js

"use strict";

angular.module("fornecedor")
.directive('manterFornecedor', function () {
    return {
        restrict: 'EA',
        templateUrl: '/Marcenaria/Scripts/App/Directive/Fornecedor/manter-fornecedor.html',
        link: function(scope,elm,attrs){

        },
        scope: {
            msgSucesso: '=',
            msgError: '=',
            msgInfo: '='
        }
    }
});

message js.

"use strict";

angular.module("layout")
.directive('mensagem', function () {
    return {
        restrict: 'EA',
        templateUrl: '/Marcenaria/Scripts/App/Directive/Layout/mensagem.html',
        scope:{
            sucesso: '=',
            error: '=',
            info: '='
        }
    }
});

html message.

<div ng-if="sucesso" class="alert alert-success alert-dismissable">
    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
    <h4><i class="icon fa fa-check"></i> Sucesso!</h4>
    {{sucesso}}
</div>
<div ng-if="error" class="alert alert-warning alert-dismissable">
    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
    <h4><i class="icon fa fa-warning"></i> Atenção!</h4>
    {{error}}
</div>
<div ng-if="info" class="alert alert-info alert-dismissable">
    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
    <h4><i class="icon fa fa-info"></i> Informativo!</h4>
    {{info}}
</div>

maintain-vendor-formulary.html

<form role="form">
    <div class="box-body">
        <div class="form-group col-xs-4">
            <label>Nome</label>
            <input type="text" class="form-control" ng-model="Nome"
                    name="Nome" id="Nome" placeholder="Insira o nome" />
        </div>
        <div class="form-group col-xs-4">
            <label>Telefone</label>
            <input type="text" class="form-control" ng-model="Telefone"
                    name="Telefone" id="Telefone" placeholder="Insira o número de telefone" />
        </div>
        <div class="form-group col-xs-4">
            <label>Contato</label>
            <input type="text" class="form-control" ng-model="Contato"
                    name="Contato" id="Contato" placeholder="Insira o nome do contato" />
        </div>
    </div>
    <div class="modal-footer">
        <botao-salvar ng-click="mFornCtrl.salvar()"></botao-salvar>
        <botao-limpar ng-click="mFornCtrl.limpar()"></botao-limpar>
    </div>
</form>

maintain-supplier-formulario.js

"use strict";

angular.module("fornecedor")
.directive('manterFornecedorFormulario', function () {
    return {
        restrict: 'EA',
        templateUrl: '/Marcenaria/Scripts/App/Directive/Fornecedor/manter-fornecedor-formulario.html',
        link: function(scope,elm,attrs){

        },
        scope:{
            msgSucesso: '=',
            msgError: '=',
            msgInfo: '='
        },
        controller: "manterFornecedorCtrl",
        controllerAs : "mFornCtrl"
    }
});

angular.module("fornecedor")
.controller("manterFornecedorCtrl", ["$scope", "$http", "$timeout", "request", "tools", function ($scope, $http, $timeout, request, tools) {

    var self = this;
    $scope.loadingAjax = false;

    this.limpar = function () {
        $scope.Nome = "";
        $scope.Contato = "";
        $scope.Telefone = "";
        $scope.msgSucesso = "";
        $scope.msgError = "";
        $scope.msgInfo = "";
    };

    this.salvar = function () {
        $scope.msgSucesso = "";
        $scope.msgError = "";
        var msg = "";

        if (tools.IsNullOrEmpty($scope.Nome)) {
            msg += "O campo Nome está em branco! \n";
        }

        if (msg.length == 0) {
            if (!$scope.loadingAjax) {
                $scope.loadingAjax = true;
                console.log("ID para alterar: "+$scope.Id);
                request._post("/Marcenaria/Fornecedor/Salvar", {
                    Nome: $scope.Nome,
                    Telefone: $scope.Telefone,
                    Contato: $scope.Contato,
                    Id: $scope.Id
                })
                .then(function (data) {
                    $scope.msgSucesso = "Forncedor inserido com sucesso.";
                    $scope.loadingAjax = false;
                }, function (erro) {
                    $scope.msgError += "ERRO: " + erro;
                    $scope.loadingAjax = false;
                });
            }
        } else {
            $scope.msgError = msg;
        }
    };
}]);
  • 1

    I don’t see much sense in what you’re trying to do. Once the directive data is compiled, it will be available in the main scope. I believe the question should be different. "How to update data from a directive in the main scope?"

  • Thanks for the north, I’ll look over.

1 answer

0


There is no need to directly change the $Scope from another module, but using the two way Binding. So it was only necessary to pass the data as attributes in the directive:

<cabecalho-interno entidade="Funcionario" acao="Pesquisar"></cabecalho-interno>

<section class="content">
    <mensagem error="msgError" sucesso="msgSucesso" info="msgInfo"></mensagem>
    <div class="col-md-12">
        <div class="box box-primary">
            <div class="box-header">
                <h3 class="box-title">Casdastro</h3>
                <div class="box-tools">
                    <button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
                </div>
            </div>
            <manter-fornecedor-formulario msg-error="msgError" msg-sucesso="msgSucesso" msg-info="msgInfo"></manter-fornecedor-formulario>
        </div>
    </div>
    <hr />
</section>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.