How to call factory methods from HTML in angularjs?

前端 未结 3 611
予麋鹿
予麋鹿 2020-12-30 20:21

In controller I have followed methods:

var isPaused  = false; 

$scope.switcher = function (booleanExpr, trueValue, falseValue) {
    return boo         


        
3条回答
  •  盖世英雄少女心
    2020-12-30 20:40

    Using AngularJS 1.7.2 and Page Controller (Every Webpage has it's own controller)

    First take the Factory view:

    AppName.factory('eventMate', function() {
        let obj = this;
    
        obj.search = "";
    
        obj.setSearchValue = function(value) {
            obj.search = value;
            return obj.search;
        };
    
        obj.getSearchValue = function() {
            return obj.search;
        };
    
        return obj;
    });
    

    In one of the child Controller, named rfcController

    AppName.controller("rfcController", function ($scope, $http, $filter, $window, $location, $anchorScroll,
                                                   textMate, datetimeMate, restpack, pagination, imageMate, eventMate) {
    
        //there are many more service providers and injectors also, just showing for an idea
        $scope.eventMate = eventMate;
    
    
        $scope.getFiltered = function(){
            let searchValue = $scope.eventMate.getSearchValue().toString();
    
            if(searchValue === undefined){
                return "";
            } else {
                return searchValue.toString();
            }
        };
    }
    

    Now see the source through html side, where I will call this method.

    {{value.createdDate}} {{value.rfcTitle}} {{value.rfcDetails}}

    Hope it will help many one. :D

提交回复
热议问题