Skip to content

Instantly share code, notes, and snippets.

@heidsoft
Last active December 19, 2016 15:01
Show Gist options
  • Select an option

  • Save heidsoft/3e1843e20d57d0f75314ee3cf16ee737 to your computer and use it in GitHub Desktop.

Select an option

Save heidsoft/3e1843e20d57d0f75314ee3cf16ee737 to your computer and use it in GitHub Desktop.
angularjs $compile 使用
$("#dynamicContent").html(
$compile(
"<button ng-click='count = count + 1' ng-init='count=0'>Increment</button><span>count: {{count}} </span>"
)(scope)
);
@heidsoft
Copy link
Author

heidsoft commented Dec 19, 2016

angular.module("myApp", [])
.controller("BadController", function($scope){
  // This variable starts as a scalar value. Danger! Danger!
  $scope.myModel = null;
  $scope.models = ["red", "white", "blue"];
})
.controller("GoodController", function($scope){
  // This object will be accessible in all isolate scope underneath this scope.
  $scope.uiState = {};
  // This variable is inside a non-scalar variable. All is good.
  $scope.uiState.myModel = null;
  $scope.uiState.models = ["red", "white", "blue"];
});

@heidsoft
Copy link
Author

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.checkUsername = function() {
    //send ajax to check on server
    if ($scope.username === 'hellobug') {
      $scope.usernameAlreadyExist = true;
    }
  };
});

app.directive('ngBlur', function($document) {
  return {
    link: function(scope, element, attrs) {
      $(element).bind('blur', function(e){
         scope.$apply(attrs.ngBlur);
      });
    }
  };
});

app.directive('repeater', function($document) {
  return {
    restrict: 'A',
    compile: function(element, attrs) {
      var template = $(element).children().clone();
      for(var i=0; i<attrs.repeater - 1; i++) {
        $(element).append(template.clone());
      }
    }
  };
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment