html怎么引入angularjs

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html怎么引入angularjs相关的知识,希望对你有一定的参考价值。

html中引入angularjs的方法:

1、在html的script标签中加入以下代码:

<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>

2、引入后调用示例:

<html ng-app="myNoteApp">
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-controller="myNoteCtrl">
<h2>我的笔记</h2>
<p><textarea ng-model="message" cols="40" rows="10"></textarea></p>
<p>
<button ng-click="save()">保存</button>
<button ng-click="clear()">清除</button>
</p>
<p>Number of characters left: <span ng-bind="left()"></span></p>
</div>
<script src="myNoteApp.js"></script>
<script src="myNoteCtrl.js"></script>
</body>
</html>

3、运行结果:

参考技术A 实在是不能对jquery的ajax方法和基于页面dom的各种取值、传值方法满意(虽然jquery已经解救过我一次了),刚好手上这个项目用jquery的方法写了一半,决定试试很久以前自学的angularJS,把现在项目中jquery得部分用angularJS重写一遍。 写的时候碰到很多问题,因为刚开始用,写法思路还是和写jquery相似,导致与在一个生成select option下拉框时花费了2个小时才最终搞定。 普通html select option, 需要该字段名称name,选线id,选项label <select name="data" id="data_select"> <option value="id1">a</option> <option value="id2">b</option> <option value="id3">c</option> </select> jquery的取选项得做法 select_id = $("#data_select option:selected").val(); jqueryde的思路大概是上面这样的,但是使用了angularJS后,着实让我迷惑了 angularJS在select下使用ng-option 标签生成选项实例 <select class="form-control" ng-model="bubble_inputs.y" ng-options="y.name for y in basic_data.frameworks_y" > 生成的html dom如下: <select class="form-control ng-valid ng-dirty" ng-model="bubble_inputs.x" ng-options="x.name for x in basic_data.frameworks_x" > <option value="0">x1</option> <option value="1">x2</option> <option value="2">x3</option> <option value="3">x4</option> ... </select> 看见这个输出,我发现没法把id写在option > value上 这岂不是无法获取选项信息了?这不行,万万不行 。结果开始了长时间的google,还是没有解决办法,就只能生成这种只有label的option,这咋办呢?难道要用ng-repeat生成option? <select class="form-control" name="y_id" id="type_y"> <option ng-repeat="framework_y in basic_data.frameworks_y" value="framework_y.id"> framework_y.name </option> </select> 我甚至开始这么尝试了,但是我的理智战胜了偷懒的邪念,决定通过ng-click取model里的值看一看,结果console.log出来一看,发现自己一个多小时都傻x了。看看console里的结果: Object bubble_name: "ab", bubble_description: "b", x: Object, y: Object, series: Object bubble_description: "b" bubble_name: "ab" series: Object x: Object y: Object date: "2014-03-11 09:03:22" description: "" disable_flag: "1" id: "9" name: "y3" position: "1" team_id: "8" type: "y" __proto__: Object __proto__: Object angularJS根据model的名称早就把每个相关的input的值自动放在Object中了 根本不在需要什么value=‘id’,只要把model的object从接口中取出来,放在html页面上,、选中的option的全部数据angularJS自动就帮我取到controller中得点击事件中去了 参考技术B <div ng-show="xs">
姓名:<input type="text" ng-model="name" /><br>
地点:<input type="text" ng-model="address" /><br>
数量:<input type="text" ng-model="num" /><br>
日期:<input type="text" ng-model="regDate" /><br>
单价:<input type="text" ng-model="price" /><br>
状态:<input type="text" ng-model="state" /><br>
<input type="button" value="保存" ng-click="save()" />
</div>
$scope.add=function()
$scope.xs=true;

$scope.save=function()
var name=$scope.name;
var address=$scope.address;
var num=$scope.num;
var regDate=$scope.regDate;
var price=$scope.price;
var state=$scope.state;
var obj=
"gname": name,
"address": address,
"num": num,
"regDate": regDate,
"price": price,
"state": state

$scope.yao.push(obj);
$scope.name="";
$scope.address="";
$scope.num="";
$scope.regDate="";
$scope.price="";
$scope.state="";
$scope.xs=false;

angularjs1-3,工具方法,bootstrap,多个module,引入jquery

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>无标题文档</title>
        <script type="text/javascript" src="angular.min.js"></script>
    </head>
    <body>
      <div ng-app="myApp">
          <div ng-controller="firstController">
              {{name}}
              {{isArray}}
              {{name1}}
              {{eq}}
          </div>
      </div>
      <script type="text/javascript">
          var app = angular.module("myApp", []);
          app.controller(firstController,[$scope,function($scope){
              /*$scope.name=‘zhangsan‘;
              $scope.arr=[1,2,3];

              $scope.isArray=angular.isArray($scope.arr);
              $scope.name1=angular.uppercase($scope.name);
              $scope.a=‘111‘;
              $scope.b=‘111‘;
              $scope.eq=angular.equals($scope.a,$scope.b);
              console.log($scope.eq);//true

              $scope.a={name:‘张三‘};
              $scope.b={age:10};
              $scope.c=angular.extend($scope.b,$scope.a);
              console.log($scope.c);//Object {age: 10, name: "张三"}

              var json = {"name":"hello","age":"20"};
              console.log(json);//Object {name: "hello", age: "20"}

              $scope.json=angular.toJson(json);
              console.log($scope.json);//{"name":"hello","age":"20"}
              $scope.json=angular.toJson(json,true);
              console.log($scope.json);

              var json = ‘{"name":"hello","age":"20"}‘;
              console.log(json);//{"name":"hello","age":"20"}
              $scope.json=angular.toJson(json);
              $scope.json=angular.fromJson(json);
              console.log($scope.json);//Object {name: "hello", age: "20"}

              $scope.a={name:‘张三‘};
              $scope.b={age:10};
              $scope.c=angular.copy($scope.a,$scope.b);
              console.log($scope.a);
              console.log($scope.b);
              
              var json = {"name":"hello","age":"20","sex":‘男‘};
              angular.forEach(json,function(val,key){
                  console.log(val);
                  console.log(key);
              });*/

              var json = {"name":"hello","age":"20","sex":};
              var results=[];
              angular.forEach(json,function(val,key){
                  console.log(val);
                  console.log(key);
                  this.push(key+--+val);
              },results);
              console.log(results);

              //绑定对象,作为函数的上下文
              var self={name:张三};
              var f=angular.bind(self,function(age){
                $scope.info=this.name+ is +age;
                console.log($scope.info);
              });
              f(30);

              var f=angular.bind(self,function(age){
                  $scope.info=this.name+ is +age;
                  console.log($scope.info);
              },10);
              f();


          }]);
      </script>
       
    </body>
</html>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <script type="text/javascript" src="angular.min.js"></script>
</head>
<body>
<div>
   <!--  <div ng-controller="firstController" ng-app="myApp1">
        {{name}}
    </div>
    <div ng-controller="secondController" ng-app="myApp2">
        {{name}}
    </div>
    var app1 = angular.module("myApp1", []);
    var app2 = angular.module("myApp2",  []);//报错,module只会初始化一次, -->

    <div id=‘div1‘ ng-controller="firstController"
        {{name}}
    </div>
    <div  id=‘div2‘  ng-controller="secondController">
        {{name}}
    </div>
</div>
//bootstrap不是css的bootstrap,一般一个页面只有一个module,bootstrap用于页面初始化多个module,
<script type="text/javascript">
    var app1 = angular.module("myApp1", []);
    var app2 = angular.module("myApp2", []);//报错,module只会初始化一次,
    app1.controller(firstController,function($scope){
        $scope.name=张三;
    });
    app2.controller(secondController,function($scope){
        $scope.name=李四;

    });
    var div1=document.getElementById(div1);
    var div2=document.getElementById(div2);
    document.onclick=function(){//动态加载多个module
        angular.bootstrap(div1,[myApp1]);
        angular.bootstrap(div2,[myApp2]);
    }
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <script type="text/javascript" src="angular.min.js"></script>
    <script type="text/javascript" src="module02.js"></script>
<!-- module02.js:
var app2 = angular.module("myApp2", []);
app2.controller(‘secondController‘,function($scope){
    $scope.name=‘李四‘;
});
app2.controller(‘threeController‘,function($scope){
    $scope.name=‘王五‘;
});-->
</head>
<body ng-app="myApp">
<div>
    <div ng-controller="firstController">
        {{name}}
    </div>
    <div ng-controller="secondController">
        {{name}}
    </div>
    <div ng-controller="threeController">
        {{name}}
    </div>
</div>
<script type="text/javascript">
    var app1 = angular.module("myApp", [myApp2])//模块的依赖,[‘myApp2‘,‘myApp3‘]可以引入多个,这是插件化引入。
    app1.controller(firstController,function($scope){
        $scope.name=张三;
    });
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    //jqueru要放在上面
    <script type="text/javascript" src="jquery-1.11.1.js"></script>
    <script type="text/javascript" src="angular.min.js"></script>
    <script type="text/javascript" src="module02.js"></script>
<!-- module02.js:
var app2 = angular.module("myApp2", []);
app2.controller(‘secondController‘,function($scope){
    $scope.name=‘李四‘;
});
app2.controller(‘threeController‘,function($scope){
    $scope.name=‘王五‘;
});-->
</head>
<body ng-app="myApp">
<div>
    <div ng-controller="firstController">
        <div id="obj1">
        </div>
        {{name}}
    </div>
    <div   ng-controller="secondController">
        {{name}}
    </div>
    <div   ng-controller="threeController">
        {{name}}
    </div>
</div>
<script type="text/javascript">
    var app1 = angular.module("myApp", [myApp2]);
    app1.controller(firstController,function($scope){
        $scope.name=张三;
       // $("#obj1").html(‘<span>尿道嗦嘎电视柜 v邓先生广东省高</span>‘);
           var obj1=document.getElementById(obj1);
            angular.element(obj1).html(这是angularjs中的jqlite);
    });
</script>
</body>
</html>

 

以上是关于html怎么引入angularjs的主要内容,如果未能解决你的问题,请参考以下文章

AngularJs

thinkphp中html页面怎么引入html页面

vue组件中怎么引入html文件

html怎么引入angularjs

vue组件中怎么引入html文件

htmlz怎么用vue路由引入其他页面