Wscats / angular-tutorial

:rabbit:Some of the angular tutorial - 《Angular学习笔记》
387 stars 125 forks source link

directive组件化开发 #4

Open Wscats opened 8 years ago

Wscats commented 8 years ago
<?php
    $ch = curl_init();
    $url = 'http://apis.baidu.com/jidichong/school_search/school_search?name=广东外语外贸大学&npage=1';
    $header = array(
        'apikey: 0aea38d1a7c4443f2f00adc86c4c3e72',
    );
    // 添加apikey到header
    curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // 执行HTTP请求
    curl_setopt($ch , CURLOPT_URL , $url);
    $res = curl_exec($ch);
    echo $res;
    //var_dump(json_decode($res));
?>
Wscats commented 8 years ago
<!DOCTYPE html>
<html ng-app="wsscat">

    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <script type="text/javascript" src="js/angular.js"></script>

    <body ng-controller="homeCtrl">
        <ul>
            <li ng-repeat="new in news">
                <p>{{new.abstract}}</p>
            </li>
        </ul>
        <button ng-click="more()">查看更多</button>
    </body>
    <script>
        var app = angular.module('wsscat', []);
        app.controller('homeCtrl', ['$scope', '$http', function($scope, $http) {
            $scope.text = '123';

            function getNews() {
                $http.get('baiduApi.php',{
                    keyword:'体育',
                    page:1,
                    count:40
                }).success(function(data) {
                    console.log(data);
                    console.log(data.retData.data);
                    console.log(data.retData.data[0]);
                    $scope.news = data.retData.data;
                })
            }

            getNews();

            $scope.more = function(){
                getNews();
            }

        }])
    </script>

</html>
Wscats commented 8 years ago

<?php
    $ch = curl_init();
    $url = 'http://apis.baidu.com/songshuxiansheng/real_time/search_news?keyword=体育&page=1&count=40';
    $header = array(
        'apikey: 0aea38d1a7c4443f2f00adc86c4c3e72',
    );
    // 添加apikey到header
    curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // 执行HTTP请求
    curl_setopt($ch , CURLOPT_URL , $url);
    $res = curl_exec($ch);

    //var_dump(json_decode($res));
    echo $res;
?>
Wscats commented 8 years ago

1.angular.element英文官网原版文档 DOM结构如下: <div id="text">213</div> 首先用JS原生的选择器,然后再传入angular.element方法里面再去调用相关的函数操作节点

var a = document.getElementById('text');
console.log(angular.element(a).html());

2.组件获取视图的数据可以用attrs,但数据要来自于表达式{{}}

link: function(scope, element, attrs) {
                    console.log(attrs);
                }

还可以用scope获取

scope: {
                    title: '=expanderTitle'
                }

对应插件中的expander-title='title',这里注意title是不用表达式{{}}转换的,如果要加表达式只能用attrs的方法来获取视图中的值,还有注意只能是根据定义的组建名字作为前缀expander-定义的属性,才能用scope获取

<expander class='expander' expander-title='title'>
            {{text}}
        </expander>

3.用组件操作DOM的时候组建一定要加载在页面的时候才能生效 所以当我们想操作页面的时候我们可以做一个空模版的组建用标签等形式放在页面进行页面的操作

4.定义组件的时候要小写 app.directive('wsscat', [function() {}]) 因为我们定义组件名字中出现大写字母的时候 app.directive('wssCat', [function() {}]) 我们在这种情况会失效

Wscats commented 8 years ago

学习AngularJs:Directive指令用法(完整版)