angular 學習筆記(1)

1.建立依賴: php

(1)經過factory的方式去建立 html

angular.module('myApp.services', []) 
  .factory('githubService', function() { 
    var serviceInstance = {}; 
    // 咱們的第一個服務 
    return serviceInstance; 
  });

調用: jquery

app.controller('ServiceController', ['$scope', '$timeout', 'githubService', 
    function($scope, $timeout, githubService) { 
}]);


注意:咱們要遵照Angular services依賴注入的規範:自定義的service要寫在內建的Angular services以後,自定義的service之間是沒有前後順序的。 git

2.$watch應用: github

$scope.$watch("變量的名字 好比:username",function(newUsername){ api

//newUsername:新的變量值 app

})
dom

3.controller控制器post

注意:不要在controller中有任何的操控DOM的行爲,若是這麼作會污染你的controller並留下潛在的隱患。  ui

4.directive 指

基本上dom結構的修改都在該狀態下執行 

myapp.directive("recheck", ["validate", function(validate) {//這裏注入validate依賴(該依賴是樓主建立的非api),
	return {
		require: "ngModel",
		scope: false,
		compile: function(tElem, attrs) {
			return function(scope, ele, attrs, ctrs) {
				ele.bind("blur", function() {
					var output = scope.userpswagain == scope.userpsw;
					scope.$apply(function() {
						//ctrs.$setValidity("validneed", output);
						scope.recheck = !output;
					})
				})
			}
		}
	}
}]);


5.angular $http 和後臺php數據交互

//.js

$http({
	headers: {
			'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
		},
				"method":"post",//或者get
				"url": "",
				"data": formdata
			}).success(function(data, status, headers, config) {
 
				console.log(data);
			}).error(function(data, status, headers, config) {
				console.log(data)
			})
//.php
 header("Content-type:text/html;charest=utf-8");
 $params=file_get_contents("php://input","r");
 echo $params;

注意  與jquery 處理方式不一樣,php不能正常讀取angular傳送過去的值,須要通過 file_get_contents("php://input","r");處理

相關文章
相關標籤/搜索