te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How to check if a input field has a value in the controller using AngularJS - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check if a input field has a value in the controller using AngularJS - Stack Overflow

programmeradmin3浏览0评论

I'm quite simply trying to find out how I can retrieve the scopes information to see if it has a value or not in the controller.

I need to be able to write an if statement that says if this text input field is filled do this... if else do something different.

<form name="deadlineForm">
    <div class="app-select-date">
        <label for="deadline">Select deadline:</label>
        <input pickadate ng-model="deadline" format="dd/mm/yyyy" placeholder="Select deadline"/>
    </div>
</form>

What is the best way about writing this code in the controller?

I'm quite simply trying to find out how I can retrieve the scopes information to see if it has a value or not in the controller.

I need to be able to write an if statement that says if this text input field is filled do this... if else do something different.

<form name="deadlineForm">
    <div class="app-select-date">
        <label for="deadline">Select deadline:</label>
        <input pickadate ng-model="deadline" format="dd/mm/yyyy" placeholder="Select deadline"/>
    </div>
</form>

What is the best way about writing this code in the controller?

Share Improve this question edited Aug 2, 2017 at 7:21 Ganesh Yadav 2,6852 gold badges33 silver badges52 bronze badges asked Aug 13, 2015 at 12:30 Max LynnMax Lynn 1,7786 gold badges25 silver badges35 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 3

You have it bound to a variable (deadline) using ng-model="deadline".

Your check in the controller bees as simple as:

if ($scope.deadline != null && $scope.deadline != "") {
    //do something
}

or this can be even simplified to

if ($scope.deadline) {
    //do something
}

Simple JSFiddle DEMO.

Will this work for you ?

<script src="https://ajax.googleapis./ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script>
  angular.module('app', [])
  .controller('MainCtrl', function ($scope) {
    
    $scope.$watch('val', function (now, old) {
      if (now) {
        $scope.result = 'there is something';
      } else {
        $scope.result = 'there is nothing';
      }
    });
  });
</script>

<div ng-app='app' ng-controller='MainCtrl'>
  <input type="text" ng-model="val">  
  <span ng-bind='result'>
</div>
  

You will get the value in the input as,

$scope.deadline

you can check as ,

if($scope.deadline) {  //or whatever condition you have to check null or empty
   //anything you want to do
}

As from what I understand here it is:

<form name="deadlineForm">
    <div class="app-select-date" ng-if="deadline && deadline != ''">
        <label for="deadline">Select deadline:</label>
        <input pickadate ng-model="deadline" format="dd/mm/yyyy" placeholder="Select deadline"/>
    </div>
    <div ng-if="!deadline && deadline == ''">do something else</div>
</form>

Please check working example : DEMO

HTML:

<p>Data {{deadline}}!</p>
<div ng-if="deadline">There is date</div>
<div ng-if="!deadline">There is no date</div>

Only for testing
<button ng-click="makeDateEmpty()">Empty data</button>

Controller:

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

app.controller('MainCtrl', function($scope) {
    $scope.deadline = '02/02/2002';
    //For testing only
    $scope.makeDateEmpty = function () {
           $scope.deadline = "";
    }
});

Using ng-model automatically binds any value entered into the input to a $scope variable of the same name.

This variable bees available to plain JavaScript within your controller when you inject a dependency upon scope into it.

So $scope.deadline will give the current value of text entered into that input.

How you check that value depends on the context of when you'd like to check it.

If you're in an existing function, such as a function checking field validation upon form submission, a simple

if ($scope.deadline) {
   // do something
} else {
  // do something else
}

would suffice.

If you're looking to trigger your logic the moment that input field changes, in the controller you could use the $watch directive, as Konpat Ta Preechakul suggested, or you could add an ng-change or ng-blur attribute to your input, and call a controller-side function:

<input pickadate ng-model="deadline" format="dd/mm/yyyy" placeholder="Select deadline" ng-change="selectedDate()"/>

and

$scope.selectedDate = function(){
    if ($scope.deadline) {
       // do something
    } else {
      // do something else
    }
}

If you want conditional logic to occur directly in the view, bound real-time to values in the input field, then ng-if, ng-show, and ng-hide can be helpful:

<span ng-show="deadline">Show this if deadline has a value</span>
<span ng-hide="deadline">Don't show this if deadline has a value</span>
<span ng-if="!deadline">Don't render this into the DOM at all if deadline doesn't have a value</span>
发布评论

评论列表(0)

  1. 暂无评论