2015-09-07 3 views
1

Я хочу, чтобы вызвать функцию контроллера из директивы после обновления моих записей ниже кода, который используетсявызова функция контроллера из директивы в angularjs

Controller.js 

    app.controller('GetAlphabetical', function ($scope, $http) { 
    function getCutomers() { 

      $scope.loading = true; 
      $http.get(ROOT + "Home/GetPesrons").then(function (response) { 
       //var _data = angular.fromJson(response); 
       $scope.loading = false; 
       $scope.Customer = response.data; // please check the request response if list id in data object 
      }, function (error) { 
       throw error; 
      }) 
     } 

     getCutomers(); 



app.directive('popOver', function ($compile, $http) { 

    var itemsTemplate = "<ul class='unstyled'><input type='text' ng-model='items.ProfaneWord' class='form-control'><button id='popeditSave' class='btn-block btn btn- danger' ng-click='popeditSave(items.ProfaneWord,items.pID, items.LanguageID)'>Save</button>"; 
    var getTemplate = function (contentType) { 
     var template = ''; 
     switch (contentType) { 
      case 'items': 
       template = itemsTemplate; 
       break; 
     } 
     return template; 
    } 
    return { 
     restrict: "A", 
     transclude: true, 
     template: "<span ng-transclude></span>", 
     link: function (scope, element, attrs) { 
      var popOverContent; 
      if (scope.items) { 
       var html = getTemplate("items"); 
       popOverContent = $compile(html)(scope); 
      } 
      scope.popeditSave = function (ProfaneWord, pID, LanguageID) { 
       var dataToPost = { proPhen: ProfaneWord, ID: pID, LangID: LanguageID }; 
       $http.post('/Home/UpdateLibrary', dataToPost) 
        .success(function (serverResponse, status, headers, config) { 


         getCutomers(); 
        }).error(function (serverResponse, status, headers, config) { 
         $("#succmsg").html("Update failure..").show(500).delay(5000).fadeOut(1000); 
        } 
       ); 
      } 




      var options = { 
       content: popOverContent, 
       placement: "bottom", 
       html: true, 
       title: scope.title 
      }; 
      $(element).popover(options); 
     }, 
     scope: { 
      items: '=', 
      title: '@' 
     } 
    }; 
}); 

}); 

Я хочу вызвать getCustomer() функции после редактирования моих записей.

+1

переместить эти данные ВОССТАНОВЛЕНИЯ функции на завод или услуги, и ввести их туда, куда вы хотите. – Jorg

+1

fomat ваш код в вашем профиле :) – Raghavendra

ответ

0

изменение

function getCutomers() {} 

в

$scope.getCutomers = function(){} 

и в директиве вызова

scope.getCutomers(); 
+0

Его неработающие друзья –

+0

можем ли мы увидеть ваш html ..? – maddygoround

+0

Хотя это, вероятно, очень хороший ответ, пожалуйста, подумайте о том, почему и как это работает. –

0

Добавьте это в ваших Directi ве

scope: { 
    items: '=', 
    title: '@', 
    getCustomer:'&getCustomer',  
} 

$scope.getCustomer()(any param from directive); //call function from controller 

вызов это от вашего GetCustomer Html

<pop-over get-Customer="getCutomers"> 

и изменения контроллера к:

app.controller('GetAlphabetical', function ($scope, $http) { 
    $scope.getCutomers = function() { 
     $scope.loading = true; 
     $http.get(ROOT + "Home/GetPesrons").then(function (response) { 
      //var _data = angular.fromJson(response); 
      $scope.loading = false; 
      $scope.Customer = response.data; // please check the request response if list id in data object 
     }, function (error) { 
      throw error; 
     }); 
    }; 

    $scope.getCutomers(); 
}); 
0

Я создал директиву, проверить имя пользователя, является уникальным или не

app.directive('ngUnique', ['$http', function ($http) { 
    return { 
     require: 'ngModel', 
     link: function (scope, elem, attrs, ctrl) { 
      elem.on('blur', function (evt) { 
       scope.$apply(function() { 
        $http({ 
         method: 'POST', 
         url: '/User/IsUserExist', 
         data: { 
          username: elem.val(), 
          dbField: attrs.ngUnique 
         } 
        }).success(function (data, status, headers, config) { 
         if (attrs.ngUnique == "username") 
          ctrl.$setValidity('unique', data); 
         else if (attrs.ngUnique == "email") 
          ctrl.$setValidity('uniqueEmail', data); 
         else if (attrs.ngUnique == "oldpassword") 
          ctrl.$setValidity('unique', data); 
         return undefined; 
        }); 
       }); 
      }); 
     } 
    } 
}]); 

пожалуйста создать директиву, как приведенные выше код, это будет работать

в моем проекте его работает нормально, чтобы проверить имя пользователя и адрес электронной почты уникального