从学习angular,到实际项目开发不到一周,完全是边写边学呀,都是为了项目,已使用angular 开发了两个项目了,有些技术当时只是会用,都没好好回顾一下,现在有时间回顾一下,项目中用到的一些指令,服务,路由,filter 等,

一点点记录一来

// 初始化
angular.bootstrap(dom,['appName']);
//html 转化
// 需传参 $sce
$scope.escape = function(html) {
   return $sce.trustAsHtml(html);
};
// html
<div ng-bind-html="escape(data)"></div>
// http
$http({
     method:'get',  // post ....
     url:'/Service/resume', // url
     params: {id: uid}
}).success(function(data){
     console.log(data)
})
// filter
.filter('getCity', function(){
        return function(city){
            return $.parseJSON(city).city;
        }
});

//html
{{city | getCity}}
//标签切换
<span class="{{curShow=='register'?'current':''}}" ng-click="switchView('register')">个人注册</span>
<span class="{{curShow=='login'?'current':''}}" ng-click="switchView('login')">个人登录</span>
<div class="{{curShow!='register'?'hide':''}}">
    // register
</div>
<div class="{{curShow!='login'?'hide':''}}">
    //login
</div>
//初始化
$scope.curShow = 'register';
$scope.switchView = function(view) {
    $scope.curShow = view;
}

//ng-click="switchView('login')"
<div class="jd">
    <label " checked="checked" id="company">企业</label>
    <label " id="personl">个人</label>
</div>

//radio 切换
<div class="jd">
   <div ng-show="isCheckboxSelected('1')">
        <label for="leader"><input type="radio" name="guanxi" id="leader">主管</label>
        <label for="hr"><input type="radio" name="guanxi" id="hr">HR</label>
   </div>
   <div ng-show="isCheckboxSelected('2')">
        <label for="workmate"><input type="radio" name="guanxi" id="workmate">同事</label>
        <label for="students"><input type="radio" name="guanxi" id="students">同学</label>
        <label for="friend"><input type="radio" name="guanxi" id="friend">朋友</label>
   </div>
</div>
 $scope.checkboxSelection = ';
$scope.isCheckboxSelected = function(index) {
    return index === $scope.checkboxSelection;
};
// factory
var app = angular.module('factory',[]);
        app.factory('testFactory', function () {
           //   return {
           //      lable: function(){
           //          return  [
                    //     {'id' : 1, 'name':'Ted', 'total': 5.996},
                    //     {'id' : 2, 'name':'Michelle', 'total': 10.996},
                    //     {'id' : 3, 'name':'Zend', 'total': 10.99},
                    //     {'id' : 4, 'name':'Tina', 'total': 15.996}
                    // ];
           //      }
           //  }

           // * edit new methods
           var data = [
                {, 'name':'Ted', 'total': 5.996},
                {, 'name':'Michelle', 'total': 10.996},
                {, 'name':'Zend', 'total': 10.99},
                {, 'name':'Tina', 'total': 15.996}
            ];
           var factory = {};
           factory.lable = function(){
                return data;
           }
           return factory;
        });

app.controller('TestController',function($scope,testFactory){
    $scope.customers = testFactory.lable();
 })
// 代码详见,github
// https://github.com/llqfront/angular/tree/master/angular
// service.js
var app = angular.module('factory',[]);
app.factory('testFactory', function ($http) {
      var factory = {};
      factory.lable = function(){
         return $http.get('/js/test.json');
      }
       return factory;
});
// controller.js
app.controller('TestController',function($scope,testFactory){
    function init(){
        testFactory.lable().success(function(data){
            $scope.customers = data;
            })
    }
    init();
})
//service、provider、factory写法
var app = angular.module('appName', []);
        app.service('testService',function(){
             this.lable = 'this is service';
        });
        app.factory('testFactory', function () {
             return{
                lable: function(){
                return 'this is factory';
                }
            }
        });
        app.provider('testProvider', function(){
            this.$get = function(){
                return 'this is provider';
            }
        });
        <body ng-controller='outputCtrl'>
            <p>{{ output1 }}</p>
            <p>{{ output2 }}</p>
            <p>{{ output3 }}</p>
        </body>
        var app = angular.module('appName');
        app.controller('outputCtrl', function($scope,testService, testFactory, testProvider){
            $scope.output1 = testService.lable;
            $scope.output2 = testFactory.lable();
            $scope.output3 = testProvider;
        });
require('lib/angular-route');//引入 route
var app = angular.module('testApp',['ngRoute','factory','ctrl']);// ngRoute
    app.config(function($routeProvider) {
         $routeProvider.when('/', {
             templateUrl: '/view/index.html',  // 模板路径
             controller: 'TestController'   // 模板 中的 controller
           })
         .when('/book', {
             templateUrl: '/view/book.html',
             controller: 'BookController'
           })
         .when('/test', {
             templateUrl: '/view/test.html',
             controller: 'txController'
           })
         .otherwise({
              redirectTo:'/'
            });
    });

今天呢在这增加一条,很多人看官网,或找资料,也包括我自己就是一个显示隐藏的功能怎么就实现不了呢

angular 显示 隐藏 ng-show ng-hide

笔者在这写几个例子你就会明白了,非repeat 内的 可以这样理解

//html
<p class="show-olive-btn" ng-click="showDetail(olive)">btn</p>
    <div class="test" ng-show="olive.oliveDetail">
        test
    </div>
// controller
$scope.olive = {};
$scope.showDetail = function(olive){
    olive.oliveDetail = ! olive.oliveDetail;
}
// 测试代码  html
{{olive}}<br/>
{{olive.oliveDetail}}
当你放上这两行就可以看出
默认olive.oliveDetail 是没有值,也相当于默认为false
当点击时 值为true false 切换
相当于改变{"oliveDetail":false}{"oliveDetail":true}
试一下就能明白了

有人说,单个的好实现如果在repeat 中如何实现呀

笔者也写一个例子

<tr ng-repeat="olive in customers | filter:searchText">
<td>
    <p class="show-olive-btn" ng-click="showDetail(olive)">btn</p>
    {{olive}} <!-- {"id":1,"name":"Ted","total":5.996,"oliveDetail":true/false} -->
    {{olive.oliveDetail}}<!-- true/false -->
    <div class="test" ng-show="olive.oliveDetail">
                        test
    </div>
</td>
</tr>
//controller 

$scope.showDetail = function(olive){
    olive.oliveDetail = ! olive.oliveDetail;
}

写法其实是一样的只是 这次的olive 变成了 ng-repeat="olive in customers" 中单项了

如果还不懂,写出来试一下就知道了

tack by $index

watch

...

待续....

最新文章

  1. Neutron 理解 (2): 使用 Open vSwitch + VLAN 组网 [Netruon Open vSwitch + VLAN Virutal Network]
  2. android 进程/线程管理(一)----消息机制的框架
  3. Struts2中上传图片案列
  4. sikuli运行出现问题:Win32Util.dll: Can&#39;t load 32-bit .dll on a AMD 64 bit platform
  5. JPA学习---第一节:JPA详解
  6. HDU 3911 Black And White (线段树区间合并 + lazy标记)
  7. HDOJ/HDU 1372 Knight Moves(经典BFS)
  8. Scala的一些语言特点
  9. 使用NSTimer实现倒计时-备
  10. poj1740 A New Stone Game
  11. 蓝桥杯-比酒量-java
  12. 自动清理SQLServerErrorLog错误日志避免太大
  13. Spring Boot快速入门
  14. Java Web项目(Extjs)报错七
  15. JQuery 中$(&quot;input:eq(0)&quot;) eq 的意思
  16. Node&lt;T&gt; 的作用
  17. LigerUi自动检索输入
  18. jquery解决file上传图片+图片预览
  19. CentOS7下让Asp.Net Core的网站自动运行
  20. android 回调函数使用简介

热门文章

  1. 关于OpenCV做图像处理内存释放的一些问题
  2. LeetCode 232 Implement Queue using Stacks
  3. Android“This Handler class should be static or leaks might occur”警告的处理方法
  4. 第二百九十六天 how can I 坚持
  5. Spring AOP Interceptor transaction is not working
  6. uc/os学习入门:在32位pc机上搭建编译环境
  7. rqnoj-105-核电站问题-dp
  8. 从0,1,2...n中统计0,1,2...9各出现了多少次【SWUN1597】
  9. poj 1191 棋盘分割 动态规划
  10. XML操作:1.XML类(http://blog.csdn.net/happy09li/article/details/7460521)