AngularJS自定义指令中的TwoWay数据绑定在ng

编程入门 行业动态 更新时间:2024-10-21 17:24:20
AngularJS自定义指令中的TwoWay数据绑定在ng-show中不起作用(TwoWay data binding in AngularJS custom directive not working in ng-show)

我的问题是$ scope.isValid没有绑定到ng-show =“{{isValid}}”,我不知道为什么。

我的目标是当isValid为真时,此跨度应该是可见的,并且当输入字段中有值时它是有效的。

<span class="warning" ng-show="{{isValid}}" style="color: red;"> Please answer the question to continue. </span>

这里是我的场景的链接。

http://plnkr.co/edit/qZ47TFg2G741PxdARiYR?p=preview

My problem is that $scope.isValid is not binded to ng-show="{{isValid}}" and I don't know why.

My goal is when isValid is true this span should be visible and it is valid when there is a value in input field.

<span class="warning" ng-show="{{isValid}}" style="color: red;"> Please answer the question to continue. </span>

Here is plunker link to my scenario.

http://plnkr.co/edit/qZ47TFg2G741PxdARiYR?p=preview

最满意答案

尝试使用这样:

'use strict';

var codeArtApp = angular.module("codeArtApp", ['ui.router']);

codeArtApp.directive('submitquestion', function($http) {
	return {
		scope: { 
			questId: '@',
			answerId: '@',
			isValid: '=ngShow',
		},
		link: function(scope, el, attrs) {
			$(el).find('.btn').on('click', function(e) {
				
				scope.sendQuesiton();

			})
		},
		controller: function($scope) {
			$scope.isValid = false;

			$scope.sendQuesiton = function () {
				$scope.validateQuestion();
       
       if ($scope.isValid) {
				alert('is Valid')
       } else {
         alert('Not Valid');
       }
			}

			$scope.validateQuestion = function() {
				if ($scope.answerId.length) {
				  console.log($scope.answerId);
					$scope.isValid = true;
				}
			}
		}
	};
}); 
  
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html ng-app="codeArtApp">

  <head>
    <script data-require="angular.js@*" data-semver="1.4.3" src="https://code.angularjs.org/1.4.3/angular.js"></script>
    <script data-require="ui-router@*" data-semver="0.2.8" src="http://angular-ui.github.io/ui-router/release/angular-ui-router.js"></script>
    <script data-require="jquery@2.1.4" data-semver="2.1.4" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script src="script.js"></script>
  </head>

  <body>
    <input type="text" ng-model="edit">
    <div data-submitquestion="" data-questId="862"  data-answer_id="{{edit}}">
      <button class="btn btn__next">Следващ въпрос</button>
      <span class="warning ng-hide" ng-show="isValid == true" style="color: red;">
						Please answer the question to continue.
				</span>
    </div>
  </body>

</html> 
  
 

I found that I have several problems. One is related to scope binding camelcase name. Also what you said about curly brackets. and missing binding data attribute isvalid="isvalid"in directive main tag.

Similar to this problem. Angular "=" scope does not work with camelCase

Here is my working scenario in Plunker

it should be isvalid: '=', insted of isValid: '=', I was missing isvalid="isvalid" in directive tag :

and no curly brackets here ng-hide="isvalid"

Working solution:

http://plnkr.co/edit/puOeoJ37LZXZ7ZRXVTKt?p=preview

更多推荐

本文发布于:2023-07-23 18:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1235617.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   绑定   指令   数据   AngularJS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!