angular 的进一步深刻理解

早上同事问我个问题,angular 的表单验证有没有啥第三方库能够用?html

这个问题,我想了下,以前我作的表单验证好像也没用到第三方的库来验证,直接用angular 内置的 directive 就能够搞定了。ng-minlength, ng-pattern ,ng-maxlength .. xxForm.$invalid 就能够了。html5

由于这个东西我之前用了不少,今天他这么一问,我才发现,有些问题,我还没真正深刻去理解。因而顺便多了解了一些。:)多交流node

---------------------------------------------------------------------angularjs

表单验证是angularJS一项重要的功能,能保证咱们的web应用不会被恶意或错误的输入破坏。Angular表单验证提供了不少表单验证指令,而且能将html5表单验证功能同他本身的验证指令结合起来使用,进而在客户端验证时提供表单状态的实时反馈。web

要使用表单验证,首先保证表单有一个name属性,通常的输入字段如最大,最小长度等,这些功能由html5表单属性提供,若是咱们想屏蔽浏览器对表单的默认验证行为,能够在表单元素上添加novalidate标记chrome

 

参考: http://www.javashuo.com/article/p-nwotxohc-gp.html浏览器

<html ng-app="App">
    <head>
        <script src="./js/angular.js"></script>
        <script>
            var app = angular.module('App', []);
            app.controller('appCtrl', function ($scope) {
                $scope.username = "fly";
                
            })
        </script>
    </head>
    <body ng-controller="appCtrl">
        <form name="userForm" novalidate>
            <input type="text" ng-minlength="6" required ng-model="username">
            <input type="submit" ng-disabled="userForm.$invalid" name="ok">
        </form>
    </body>
</html>

  

---------------------------------------------------------------------------------------------------------------------------------app

顺便多了解了一下,在dev tools 的console 中查看angular application 的方法:ide

How do I access the $scope variable in browser's console using AngularJS?

 

I would like to access my $scope variable in Chrome's JavaScript console. How do I do that?ui

I can neither see $scope nor the name of my module myapp in the console as variables.

For debugging I usually set window.MY_SCOPE = $scope; first thing in my controller function. 

If you're considering development/testing in Firefox, you can also use AngScope, a small extension that displays $scope objects of selected DOM elements into Firebug's DOM Inspector. – Kos Prov 

 

-------------------------------------------------------------------------------------------------------------------------------------

 

Pick an element in the HTML panel of the developer tools and type this in the console:

angular.element($0).scope()

In WebKit and Firefox, $0 is a reference to the selected DOM node in the elements tab, so by doing this you get the selected DOM node scope printed out in the console.

You can also target the scope by element ID, like so:

angular.element(document.getElementById('yourElementId')).scope()

Addons/Extensions

There are some very useful Chrome extensions that you might want to check out:

  • Batarang. This has been around for a while.

  • ng-inspector. This is the newest one, and as the name suggests, it allows you to inspect your application's scopes.

Playing with jsFiddle

When working with jsfiddle you can open the fiddle in show mode by adding /show at the end of the URL. When running like this you have access to the angular global. You can try it here:

http://jsfiddle.net/jaimem/Yatbt/show

jQuery Lite

If you load jQuery before AngularJS, angular.element can be passed a jQuery selector. So you could inspect the scope of a controller with

angular.element('[ng-controller=ctrl]').scope()

Of a button

 angular.element('button:eq(1)').scope()

... and so on.

You might actually want to use a global function to make it easier:

window.SC = function(selector){ return angular.element(selector).scope(); };

Now you could do this

SC('button:eq(10)') SC('button:eq(10)').row // -> value of scope.row

Check here: http://jsfiddle.net/jaimem/DvRaR/1/show/

相关文章
相关标签/搜索