angularjs-scope

Template always compiles with old scope value in directive

独自空忆成欢 提交于 2019-12-20 01:19:07
问题 I've got a directive that's working like this: http://jsfiddle.net/smithkl42/cwrgLd0L/23/ App.directive('prettify', ['$compile', function ($compile) { var templateFn; return { restrict: 'E', scope: { target: '=' }, link: function (scope, element, attrs) { if (!templateFn) { var template = element.html(); templateFn = $compile(template); } scope.$watch('target', function (newVal, oldVal) { var compiled = templateFn(scope); element.html(''); element.append(compiled); var html = element.html();

Creating a generic angularjs listController

亡梦爱人 提交于 2019-12-19 21:59:06
问题 Angular ui-router allows me to resolve different injections that I can use in my controllers. I created a simple generic ListController, and in the ui-router I am injecting an object that contains the CRUD functions specific for that particular view, along with the template. So, for example, let's say I have 2 services with the same "crud" syntax: addPerson(person, callback){} deletePerson(person, callback){} editPerson(person, callback){} .... addCar(car, callback){} deleteCar(car, callback)

Sharing Data between pages in AngularJS returning empty

三世轮回 提交于 2019-12-19 21:05:37
问题 Typically i write SPA's and sharing data between controllers is simple with a service. I am not using an SPA format (not using ng-view), and trying to share data between pages but on load of the second page (to get data) its empty. PAGE1 (index.html): <div ng-controller="CreateList"> <input type="text" ng-model="myValue"> <div ng-click="share(myValue)">Share</div> </div> PAGE2: <div ng-controller="GeList"> <p>{{ sharedData }}</p> </div> JS: app.controller('CreateList', function($scope,

Passing variable from controller scope to directive

核能气质少年 提交于 2019-12-19 13:10:19
问题 In my controller I've defined $scope.worker which is a plain JS object: { name: 'Peter', phone: 601002003 } I've created a directive: .directive('phoneType', [function () { return { restrict: 'A', link: function (scope, element, attrs) { console.log(attrs); } }; }]) and my HTML looks like this: <span phone-type="worker.phone"></span> How do I pass worker.phone (in this example 601002003) from the controller scope to the directive, so I can create my logic in the link method? attrs.phoneType

Passing variable from controller scope to directive

允我心安 提交于 2019-12-19 13:09:11
问题 In my controller I've defined $scope.worker which is a plain JS object: { name: 'Peter', phone: 601002003 } I've created a directive: .directive('phoneType', [function () { return { restrict: 'A', link: function (scope, element, attrs) { console.log(attrs); } }; }]) and my HTML looks like this: <span phone-type="worker.phone"></span> How do I pass worker.phone (in this example 601002003) from the controller scope to the directive, so I can create my logic in the link method? attrs.phoneType

Conditional binding with AngularJS, concatenate and bind value if property is not empty

别来无恙 提交于 2019-12-19 12:28:10
问题 I am new to Angular and am trying to bind a string to a model if the value !== empty. This work for one input, but I would like to combine multiple text inputs into one string. <input type="text" ng-model="data.source"> <input type="text" ng-model="data.medium"> <span ng-show="data.source"><h3>{{'additionToSource' + data.source}}</h3></span> <span ng-show="data.medium"><h3>{{'additionToMedium' + data.medium}}</h3> 回答1: Live demo here (click). You could simply add the ng-show or ng-hide

$watch not working on variable from other controller?

余生颓废 提交于 2019-12-19 10:16:56
问题 I have one controller which displays a checklist, and stores the selection in an array. My other controller runs an $http.get on the array from the first controller. How do I set a $watch so that whenever the array changes, a new HTTP GET request is sent? My attempt: http://plnkr.co/edit/EaCbnKrBQdEe4Nhppdfa // See plnkr for other controller + FooSelection factory + view function SimpleQueryResCtrl($scope, $http, FooSelection) { $scope.foo_list_selection = FooSelection; $scope.$watch('foo

Controller function getting called twice using ng-show

我怕爱的太早我们不能终老 提交于 2019-12-19 07:53:05
问题 I have a very simple Angular app setup, code as follows: index.html <!DOCTYPE html> <html> <head> <script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js'></script> <script src='app.js'></script> </head> <body ng-app="app"> <div ng-controller="MyCtrl"> <div ng-show="ready()"> Some content </div> </div> </body> </html> And app.js var app = angular.module('app', []); app.controller('MyCtrl', function($scope) { console.log("MyCtrl called") $scope.ready = function() {

Explaining the order of the ngModel pipeline, parsers, formatters, viewChangeListeners, and $watchers

≡放荡痞女 提交于 2019-12-19 05:57:09
问题 It's not easy to frame this question, so I will try to explain what I want to know with an example: Consider this simple angularjs app : PLUNKER angular.module('testApp', []) .controller('mainCtrl', function($scope) { $scope.isChecked = false; }) .directive("testDirective", function () { return { restrict: 'E', scope: { isChecked: '=' }, template: '<label><input type="checkbox" ng-model="isChecked" /> Is it Checked?</label>'+ '<p>In the <b>directive\'s</b> scope <b>{{isChecked?"it\'s checked"

Angular UI Modal 2 Way Binding Not Working

霸气de小男生 提交于 2019-12-19 05:32:18
问题 I am adding an Angular UI Modal where I am passing the scope through to the Modal Window for 2 way binding. I used the resolve method to pass the scope value. Doing so works sort of works meaning when the ng-model value changes in parent, it reflects inside the modal window. However, if the value changes inside the modal window, it is not reflecting in the parent ng-model. Here is my code: HTML: <div ng-app="app"> <div ng-controller="ParentController"> <br /> <input type="text" ng-model=