如何传递价值?

时间:2015-08-03 14:59:46

标签: angularjs

我有指示:

angular.module("mainModule").directive('ngEnter', function () {
    return function (scope, element, attrs) {
        element.bind("keydown", function (event) {
            if (event.which === 13) {
                scope.$apply(function () {
                    scope.$eval(attrs.ngEnter);
                });

                event.preventDefault();
            }
        });
    };
});

我有输入字段:

 <input type="search" ng-model="searchText" ng-model-options="{ debounce: 1000 }" placeholder="@Translator.Translate("SEARCH_OFFER")" ng-keydown="checkKeyDown($event)" ng-change="search()" ng-enter="Search(searchText)">

问题是,当我点击输入时,我无法将值searchText传递给控制器​​,其未定义...任何建议?

编辑:它不是减少,因为当我删除它我得到相同的结果。还有其他建议吗?

2 个答案:

答案 0 :(得分:3)

试试这个代码段:

&#13;
&#13;
angular.module('testApp', [])

.controller('homeCtrl', ['$scope', function($scope) {
	$scope.getVal = function(event) {
		var commentEl = angular.element(event.target);
		if (event.keyCode === 13) {
			alert(commentEl.val());
		}

	};
}])
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="testApp">
    <section ng-controller="homeCtrl">
        <input ng-keydown="getVal($event)">
    </section>
</body>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

 ng-model-options="{ debounce: 1000 }"

我希望您在范围值更改之前等待一秒钟。