简体   繁体   中英

How can i bind model in angular directive

I am using angular type ahead from here

http://pineconellc.github.io/angular-foundation/#/typeahead

I have made a directive where template is

<div>
    <input type="text"
           ng-model="user.selected"
           placeholder="Type to select . . ."
           typeahead="item as item.name for item in getData($viewValue)"
           typeahead-loading="loadingData"
           class="form-control">
    <i ng-show="loadingData" class="glyphicon glyphicon-refresh"></i>
</div>

and directive is

return {
    restrict: 'AE',
    replace: true,
    scope: true,
    templateUrl: 'type.html',
    link: function (scope, elem, attrs) {
        var url = window.location.origin + attrs.url;

        scope.getData = function (val) {
            return $http.get(url, {
                params: {
                    q: val
                }
            }).then(function (response) {
                return response.data.map(function (item) {
                    return item;
                });

            });
        };

    }
};

i am using like this

<my-directivw url="api/user"> </my-directive>

Now instead of writing ng-model="user.selected" in template . i want to something like this

ng-model="somevar"

<my-directivw url="api/user" somevar="user.selected"> </my-directive>

How can i do that

It might help you.

The HTML code look like this.

<div ng-app="app" ng-controller="Ctrl as ctrl">
    <form name="theform">
        <range ng-model="ctrl.theRange" name="myRange" required="true"></range>
    </form>
    <button ng-click="ctrl.theRange = '10/100'">SET</button>
</div>

The JavaScript code look like this

var app = angular.module("app",[]);

app.controller("Ctrl", function($scope) {
    this.theRange = "1/7";
});

app.directive("range", function() {
    var ID=0;

    function constructRangeString(from, to) {
        var result;
        if( !from && !to ) {
            result = null;
        }
        else if( from ) {
            result = from + "/" + (to || "");
        }
        else if( to ) {
            result = "/" + to;
        }
        return result;
    }

    return {
        require: "ngModel",
        restrict: "E",
        replace: true,
        scope: {
            name: "@"
        },
        template:
            '<div ng-form="{{ subFormName }}">' +
                '<input type="text" ng-model="from" class="range-from" />' +
                '<input type="text" ng-model="to" class="range-to" />' +
            '</div>',
        link: function(scope,elem,attrs,ngModel) {
            var re = /([0-9]+)\/([0-9]+)/;

            if( scope.name ) {
                scope.subFormName = scope.name;
            }
            else {
                scope.subFormName = "_range" + ID;
                ID++;
            }

            ngModel.$render = function() {
                var result, from, to;
                result = re.exec(ngModel.$viewValue);
                if( result ) {
                    from = parseInt(result[1]);
                    to = parseInt(result[2]);
                }
                scope.from = from;
                scope.to = to;
            };

            scope.$watch("from", function(newval) {
                var result = constructRangeString(newval, scope.to);
                ngModel.$setViewValue(result);
            });
            scope.$watch("to", function(newval) {
                var result = constructRangeString(scope.from, newval);
                ngModel.$setViewValue(result);
            });
        }
    };
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM